【问题标题】:DataKeyValues are null in gridview网格视图中的 DataKeyValues 为空
【发布时间】:2013-04-10 18:18:39
【问题描述】:

这是我的gridview 以及当用户单击行内的编辑按钮时触发的事件。出于某种原因,我的 datakey 值正在使程序崩溃,说它为空。我不知道为什么。 gridview 中的 DataKeyNames 正是我想要的,formID。这是正确的,因为如您所见,gridview 中的最后一列向我显示了formID,它显示得很好。所以我不知道我哪里出错了。

 <asp:GridView ID="gvHistory" runat="server" DataSourceID="ObjectDataSource" 
                AutoGenerateColumns="False" CellPadding="10" CellSpacing="5" 
                CssClass="userHistory" DataKeyNames="formID">
                <Columns>
                    <asp:BoundField DataField="clientName" HeaderText="User" />
                    <asp:BoundField DataField="formName" HeaderText="Form" />
                    <asp:BoundField DataField="dateCreated" HeaderText="Date Created" />
                    <asp:BoundField DataField="dateRevised" HeaderText="Last Revision Date" />
                    <asp:BoundField DataField="dateSubmitted" HeaderText="Date Submitted" />
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:Button ID="edit" runat="server" CausesValidation="false" CommandName="editForm" 
                                Text="Edit" OnDataBinding="btnEdit_DataBinding" OnClick="btnEdit_Click" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:Button ID="delete" runat="server" CausesValidation="false" CommandName=""
                                Text="Delete" OnDataBinding="btnDelete_DataBinding" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="formID" />
                </Columns>
            </asp:GridView>

protected void btnEdit_Click(object sender, System.EventArgs e)
{
    Session["formID"] = gvHistory.SelectedDataKey.Value;
    Label1.Text = Session["formID"].ToString();

}

【问题讨论】:

    标签: c# asp.net datakey


    【解决方案1】:

    如果您在项目模板中使用CommandName,那么您可以简单地使用Row_Command 事件,您不需要为按钮单击创建单独的处理程序。

    protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName == "editForm")
        {
          GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
          string value=grdView.DataKeys[row.RowIndex].Values["myDataKey"].ToString();
        }
    }
    

    【讨论】:

    • 谢谢。这工作得很好。对不起,我什至不得不问这个问题,学期末,压力越来越大。我应该能够得到这个。
    【解决方案2】:

    Button btn = (Button)sender;

            GridViewRow gvrow = (GridViewRow)btn.NamingContainer;
            if (gvrow != null)
            {
                //Get the Row Index
                int rowIndex = gvrow.RowIndex;
                //Get the DataKey value
                Session["formID"] = gvHistory.DataKeys[rowIndex].Value.ToString();
                Label1.Text = Session["formID"].ToString();
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2017-02-23
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      相关资源
      最近更新 更多