簡體   English   中英

將GridView編輯限制為僅一列

[英]Limit GridView edit to only one column

我有這樣一個gridview設置:

<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包含用戶及其角色的列表,在選擇“編輯”后,角色列單元格將變為包含角色的下拉列表(當前僅是Admin和Member)。

我當前要解決的問題是將適當的值從gridview(用戶名和下拉列表中的選定值)獲取到更新查詢的參數中。

當前代碼隱藏:

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);
}

我設法很好地獲得了下拉列表的選定值,但是我無法獲得用戶名,而是只得到一個空字符串。 我假設這是因為在單擊“編輯”后,“用戶名”字段變成了一個文本框。

我如何防止編輯用戶名列,以使單元格不會變成文本框(我只希望角色列可編輯)。 並且這是否會自動解決通過row.Cells[0].Text獲取值的問題,或者我在這里還缺少其他東西嗎?

編輯:問題2 。:參數和查詢

我的更新命令如下所示:

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

代碼隱藏參數:

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上的更新后,輸出中的調試值為:

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


?
?
0

我正在使用? 由於使用AccessDataSource Web服務器控件檢索數據中的這一行: Because the AccessDataSource control extends the SqlDataSource class and uses the System.Data.OleDb provider, you specify parameter placeholders using the "?" placeholder character. The System.Data.OleDb provider does not support named parameters; Because the AccessDataSource control extends the SqlDataSource class and uses the System.Data.OleDb provider, you specify parameter placeholders using the "?" placeholder character. The System.Data.OleDb provider does not support named parameters;

奇怪的是,有兩個空行,然后是兩個占位符名稱。 AccessDataSourceUsers.UpdateParameters[0]應該返回參數的值, AccessDataSourceUsers.UpdateParameters[0].Name返回名稱? 這兩個空行應該是值嗎? 如果是這樣,為什么它們是空的?

如何將綁定字段替換為另一個模板字段? 您的標記應如下所示: 編輯2:關於第二個問題---

  1. 我會檢查我的UpdateCommand是否有多余的行。 在您的命令中看不到封閉的引號。 我有這個UpdateCommand UpdateCommand="UPDATE users SET users.roleID = DLookup('[id]','roles', '[role]'=?) WHERE username = ?" 並且在調試中沒有任何空行。

  2. 要查看該值,您必須獲取參數的DefaultValueAccessDataSourceUsers.UpdateParameters[0].DefaultValue

編輯:我認為您不能在AccessDatasource_Updating中設置參數。 GridView的RowUpdating是執行此操作的更好位置。

   <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>

您可以在代碼的模板字段中找到標簽:

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();
    }
}

我已經測試了上面的代碼。 希望能幫助到你!

解決方案非常簡單,在設計模式下打開aspx文件,然后編輯GridView如下圖所示 編輯GridView列

然后將您希望不可編輯的列標記為只讀,如下所示

只讀-> true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM