限制编辑的GridView只有一个列 [英] Limit GridView edit to only one column

查看:122
本文介绍了限制编辑的GridView只有一个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview设置是这样的:

I have a gridview set up like this:

<asp:GridView ID="GridViewUsers" runat="server" 
    DataSourceID="AccessDataSourceUsers"
    AllowSorting="True" AutoGenerateColumns="False" AutoGenerateEditButton="True">
    <Columns>
        <asp:BoundField DataField="username" HeaderText="username" 
            SortExpression="username" />
        <asp:TemplateField HeaderText="role" SortExpression="role">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownListRoles" runat="server" 
                    DataSourceID="AccessDataSourceRoles" DataTextField="role" DataValueField="role" 
                    SelectedValue='<%# Bind("role") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("role") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

所以在GridView包含用户的列表和自​​己的角色,在选择编辑角色列单元格变成含有角色下拉列表中(目前只有管理员和会员)。

So the gridview contains a list of users and their roles, upon selecting edit the roles column cells turn into dropdown lists containing roles (currently only Admin and Member).

我目前正在处理越来越在GridView适当的值(用户名,并从下拉列表中选择值)到更新查询的参数。

The issue I'm currently tackling is getting the appropriate values from the gridview (username and selected value from the dropdown list) into the parameters of the update query.

当前codebehind:

Current codebehind:

protected void AccessDataSourceUsers_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    GridViewRow row = GridViewUsers.Rows[GridViewUsers.EditIndex];
    DropDownList ddl = (DropDownList)row.FindControl("DropDownListRoles");

    AccessDataSourceUsers.UpdateParameters.Add("@role", ddl.SelectedValue);
    AccessDataSourceUsers.UpdateParameters.Add("@username", row.Cells[0].Text);
}

我设法将DropDownList的选定值就好了,但是我不能得到用户名,而不是我只得到一个空字符串。我猜想这是因为在单击编辑用户名字段变成一个文本框。

I managed to get the selected value of the dropdownlist just fine, however I can't get the username, instead I only get an empty string. I'm assuming that's because upon clicking edit the username field turns into a textbox.

我怎么能prevent编辑username列,使细胞不变成一个文本框(我只希望为角色列可编辑)。同时还将自动修复通过获取值的问题 row.Cells [0]。文本还是有别的东西,我在这里丢失?

How could I prevent editing the username column so that the cells don't turn into a textbox (I only wish for the roles column to be editable). And also will that automatically fix the problem of getting the value through row.Cells[0].Text or is there something else I'm missing here?

编辑:第2期:参数和查询

我的更新命令是这样的:

My update command looks like this:

UpdateCommand="UPDATE users
 SET users.roleID = DLookup( &quot;[id]&quot; , &quot;roles&quot; , &quot;[role] = '?'&quot;)
WHERE ((username = '?'));

codebehind参数:

Codebehind parameters:

protected void GridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridViewUsers.Rows[GridViewUsers.EditIndex];
    DropDownList ddl = (DropDownList)row.FindControl("DropDownListRoles");
    Label lbl = (Label)row.FindControl("LabelItemEditUsername");

    if (ddl != null && ddl.SelectedValue != null && lbl != null)
    {
        AccessDataSourceUsers.UpdateParameters.Add("?", System.Data.DbType.String, ddl.SelectedValue); // I've also tried this without specifying the DbType
        AccessDataSourceUsers.UpdateParameters.Add("?", System.Data.DbType.String, lbl.Text);
        int result = AccessDataSourceUsers.Update();
        System.Diagnostics.Debug.WriteLine(AccessDataSourceUsers.UpdateCommand);
        System.Diagnostics.Debug.WriteLine(AccessDataSourceUsers.UpdateParameters[0]);
        System.Diagnostics.Debug.WriteLine(AccessDataSourceUsers.UpdateParameters[1]);
        System.Diagnostics.Debug.WriteLine(result);
    }
    else
    {
        e.Cancel = true;
    }
}

点击输出GridView中调试值更新后是:

After clicking update on gridview the debug values in output are:

UPDATE users
 SET users.roleID = DLookup( "[id]" , "roles" , "[role] = '?'")
WHERE ((username = '?'));


?
?
0

我用,由于这一行的检索数据使用AccessDataSource Web服务器控件的:因为AccessDataSource控件扩展的SqlDataSource类并使用System.Data.OleDb供应商,指定使用参数占位符?占位符。 System.Data.OleDb提供不支持命名的参数;

什么是奇怪的是,有两个空行,然后两个占位符名称。是不是 AccessDataSourceUsers.UpdateParameters [0] 应该返回参数的值, AccessDataSourceUsers.UpdateParameters [0] .Name点返回的名字吗?是两个空行应该是价值观?如果是这样,为什么他们是空的?

What is weird is that there are two empty lines and then the two placeholder names. Isn't AccessDataSourceUsers.UpdateParameters[0] supposed to return the value of the parameter, and AccessDataSourceUsers.UpdateParameters[0].Name return the name? Are the two empty lines supposed to be values? If so why are they empty?

推荐答案

如何是关于另一个模板现场更换绑定字段?您的标记应该是这样的:
修改2:关于你提到的第二个问题---

How's about replacing the bound field with another template field? Your markup should look like this: EDIT 2 : Regarding your second question---


  1. 我会检查我的更新命令为多余的线条。我看不到
    封闭的报价在你的命令。我有这样的UpdateCommand
    更新命令=UPDATE设置用户users.roleID =
    使用DLookup([ID]','角色','[作用] =?)其中username =?
    和不
    在调试的空行。

  1. I would check my UpdateCommand for the extra lines. I can't see the closed quotation in your command. I have this UpdateCommand UpdateCommand="UPDATE users SET users.roleID = DLookup('[id]','roles', '[role]'=?) WHERE username = ?" and don't have any empty line in debug.

要看到的价值,你必须得到默认值参数
像: AccessDataSourceUsers.UpdateParameters [0] .DefaultValue

To see the value, you have to get DefaultValue of the parameter like :AccessDataSourceUsers.UpdateParameters[0].DefaultValue.

编辑:我不认为你可以设置参数AccessDatasource_Updating。 GridView的RowUpdating 是更好的地方来做到这一点。

EDIT : I don't think you can set the parameters in AccessDatasource_Updating. GridView's RowUpdating is better place to do this.

   <asp:GridView ID="GridViewUsers" runat="server" 
    DataSourceID="AccessDataSourceUsers"
    AllowSorting="True" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowUpdating="GridViewUsers_RowUpdating">
    <Columns>        
        <asp:TemplateField HeaderText="username" SortExpression="username">
            <EditItemTemplate>
                 <asp:Label ID="lblUserName" runat="server" Text='<%# Bind("username") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblUserName" runat="server" Text='<%# Bind("username") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="role" SortExpression="role">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownListRoles" runat="server" 
                    DataSourceID="AccessDataSourceRoles" DataTextField="role" DataValueField="role" 
                    SelectedValue='<%# Bind("role") %>'>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("role") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
   </asp:GridView>

您可以在模板字段标签在code:

You can find the label in template field in code:

protected void AccessDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
  // Moved code to the method below
}

protected void GridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridViewUsers.Rows[GridViewUsers.EditIndex];
    var ddl = row.FindControl("DropDownListRoles") as DropDownList;
    var lblUserName = row.FindControl("lblUserName") as Label;
    if (ddl != null && ddl.SelectedValue != null && lblUserName != null)
    {
        AccessDataSourceUsers.UpdateParameters.Add("@role", ddl.SelectedValue);
        AccessDataSourceUsers.UpdateParameters.Add("@username", lblUserName.Text);
        AccessDataSourceUsers.Update();
    }
}

我已经测试过上面的code。希望它能帮助!

I have tested the code above. Hope it helps!

这篇关于限制编辑的GridView只有一个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆