且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在选中该复选框的gridview中获取值.

更新时间:2023-10-12 16:37:52

嗨拉希德,

您可以使用网格视图的行命令事件.我已将row命令事件放入您的网格中.您正在使用p_id和u_id.在行命令中获取该ID并将其保存到数据库.在需要时从数据库检索.
Hi Rashid,

You can use Row command event of grid view. I have put the row command event in your grid. you are having p_id and u_id . get that ids at row command and the save it to database. retrieve from database when you want.
<asp:GridView ID="Grid1" runat="server" BackColor="#DEBA84"

        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"

        CellSpacing="2" Width="90%" Height="10%"

        AutoGenerateColumns="False"

                        AllowPaging="True" AllowSorting="True" PageSize="5"

            onpageindexchanging="Grid1_PageIndexChanging"

            onrowediting="Grid1_RowEditing" onrowupdating="Grid1_RowUpdating" OnRowCommand="Grid1_RowCommand"

            DataKeyNames ="P_id, U_id" >


受保护的void gvCheck_RowDataBound(object sender,GridViewRowEventArgs e)
{
DataTable dt = ViewState ["chk"] as DataTable;

如果(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk =(CheckBox)e.Row.FindControl("chk");
布尔标志= Convert.ToBoolean(dt.Rows [e.Row.RowIndex] ["Chk"].ToString());//查找表中包含复选框值的列
如果(flag == true)
{
chk.Checked = true;
}

}<
}

我希望这会对您有所帮助
protected void gvCheck_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = ViewState["chk"] as DataTable;

if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chk");
Boolean flag = Convert.ToBoolean(dt.Rows[e.Row.RowIndex]["Chk"].ToString());// find column of table which contain value for check box
if (flag == true)
{
chk.Checked = true;
}

}<
}

i hope this will help u


拉希德,

您可以在说"复选框的网格中添加一列,现在每个复选框将向用户显示当前行的ID.

现在,当用户检查该复选框时,将触发一个事件,因此,您可以将与该复选框关联的ID存储到某个hiddenfield,或者如果只需要对check事件进行检查,则可以从服务器端进行回发和调用保存状态方法复选框.

而当您加载网格时,只需将选中/取消选中的值显示在复选框中即可.

让我知道您是否有疑问.
Hi Rashid,

You can add a column in grid say checkbox, now each checkbox will have id of the current row shown to user.

Now when user checks that checkbox an event will be fired so at that time you can store id which is associated with the checkbox to some hiddenfield or you can made postback and call save state method from server side if it is just required on check event of checkbox.

and when you load the grid just show the value as per check/uncheck into checkbox.

Let me know if you have a question.