SelectedRow.Cell[0]
当 GridView 中的 CommandField 为“选择”时,如何在会话中存储值。? 我将在我的代码中进行哪些更改,以便在我的网格上执行选择命令时(通过单击我的网格的选择链接),它NatureOfWorkID
将该行的存储在会话变量中。下面是我的代码。请指导我在下面的代码中需要进行哪些更改,以便当有人单击 Select 时,该行的 natureofworkID 将存储在 Session 中,我将根据我的要求从 session 变量绑定另一个网格。
<asp:GridView ID="GVNatureOFWork" runat="server" AllowPaging="true" ShowFooter="true" PageSize="10" AutoGenerateColumns="false"
DataKeyNames="NatureOfWorkID"
OnPageIndexChanging="GVNatureOFWork_PageIndexChanging"
onrowcancelingedit="GVNatureOFWork_RowCancelingEdit"
onrowcommand="GVNatureOFWork_RowCommand" onrowdeleting="GVNatureOFWork_RowDeleting"
onrowediting="GVNatureOFWork_RowEditing" onrowupdating="GVNatureOFWork_RowUpdating"
OnSelectedIndexChanged="GVNatureOFWork_SelectedIndexChanged" OnRowDataBound="GVNatureOFWork_RowDataBound"
HeaderStyle-BackColor="BlueViolet"
HeaderStyle-ForeColor="White" BackColor="White">
<Columns>
<asp:TemplateField HeaderText="NatureOfWorkID">
<ItemTemplate>
<asp:Label ID="lblNautureOfWorkID" runat="server" Text='<%#Eval("NatureOfWorkID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nature of Work Name">
<ItemTemplate>
<asp:Label ID="lblNatureOfWorkName" runat="server" Text='<%#Eval("NatureOfWorkName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNatureOfWorkName" runat="server" Text='<%#Eval("NatureOfWorkName") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddNatureOfWorkName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enable/Disable">
<ItemTemplate>
<asp:Label ID="lblNOWStatus" runat="server" Text='<%# (int)Eval("IsNOWEnabled") == 1 ? "Enabled" : "Disabled" %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="radiolistStatus" runat="server">
<asp:ListItem Text="Enable" Value="1"></asp:ListItem>
<asp:ListItem Text="Disable" Value="0"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="radiolistStatusAdd" runat="server">
<asp:ListItem Text="Enable" Value="1"></asp:ListItem>
<asp:ListItem Text="Disable" Value="0"></asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:CommandField ShowSelectButton="True" ItemStyle-CssClass="HiddenColumn" HeaderStyle-CssClass="HiddenColumn"/>
</Columns>
</asp:GridView><br /><br /><br /><br />
<asp:GridView ID="GVRegionProject" runat="server" AllowPaging="true" ShowFooter="true" PageSize="10" AutoGenerateColumns="false"
OnPageIndexChanging="GVRegionProject_PageIndexChanging"
onrowcancelingedit="GVRegionProject_RowCancelingEdit"
onrowcommand="GVRegionProject_RowCommand" onrowdeleting="GVRegionProject_RowDeleting"
onrowediting="GVRegionProject_RowEditing" onrowupdating="GVRegionProject_RowUpdating"
OnSelectedIndexChanged="GVRegionProject_SelectedIndexChanged"
HeaderStyle-BackColor="BlueViolet"
HeaderStyle-ForeColor="White" BackColor="White">
<Columns>
<asp:TemplateField HeaderText="Region/Project ID">
<ItemTemplate>
<asp:Label ID="lblRegionProjectID" runat="server" Text='<%#Eval("RegionProjectID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Region/Project Name">
<ItemTemplate>
<asp:Label ID="lblRegionProjectName" runat="server" Text='<%#Eval("RegionProjectName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRegionProjectName" runat="server" Text='<%#Eval("RegionProjectName") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddRegionProjectName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enable/Disable">
<ItemTemplate>
<asp:Label ID="lblRPStatus" runat="server" Text='<%# (int)Eval("IsRegionProjectEnabled") == 1 ? "Enabled" : "Disabled" %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="radiolistStatus" runat="server">
<asp:ListItem Text="Enable" Value="1"></asp:ListItem>
<asp:ListItem Text="Disable" Value="0"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="radiolistStatusAdd" runat="server">
<asp:ListItem Text="Enable" Value="1"></asp:ListItem>
<asp:ListItem Text="Disable" Value="0"></asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:CommandField HeaderText="County" ShowHeader="false" ShowSelectButton="True"/>
</Columns>
</asp:GridView>
后面的代码是:-
private void BindGrid()
{
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM natureOfWork";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
GVNatureOFWork.DataSource = dataTable;
GVNatureOFWork.DataBind();
}
protected void GVNatureOFWork_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GVNatureOFWork.PageIndex = e.NewPageIndex;
BindGrid();
}
protected void GVNatureOFWork_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVNatureOFWork.EditIndex = -1;
BindGrid();
}
protected void GVNatureOFWork_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtAddNatureOfWorkName = (TextBox)GVNatureOFWork.FooterRow.FindControl("txtAddNatureOfWorkName");
RadioButtonList radiolistStatusAdd = (RadioButtonList)GVNatureOFWork.FooterRow.FindControl("radiolistStatusAdd");
if ((txtAddNatureOfWorkName.Text != "")&&(radiolistStatusAdd.SelectedValue != string.Empty))
{
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO NatureOFWork(NatureOFWorkname, isNOWEnabled ) Values('" + txtAddNatureOfWorkName.Text + "', '" + radiolistStatusAdd.SelectedValue.ToString() + "')";
conn.Open();
cmd.ExecuteNonQuery();
}
BindGrid();
conn.Close();
}
if (e.CommandName.Equals("Select"))
{
}
}
protected void GVNatureOFWork_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblNautureOfWorkID = (Label)GVNatureOFWork.Rows[e.RowIndex].FindControl("lblNautureOfWorkID");
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM NatureOFWork WHERE NatureOfWorkID='" + lblNautureOfWorkID.Text.ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
BindGrid();
}
protected void GVNatureOFWork_RowEditing(object sender, GridViewEditEventArgs e)
{
GVNatureOFWork.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GVNatureOFWork_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblNautureOfWorkID = (Label)GVNatureOFWork.Rows[e.RowIndex].FindControl("lblNautureOfWorkID");
TextBox txtNatureOfWorkName = (TextBox)GVNatureOFWork.Rows[e.RowIndex].FindControl("txtNatureOfWorkName");
RadioButtonList radiolistStatus = (RadioButtonList)GVNatureOFWork.Rows[e.RowIndex].FindControl("radiolistStatus");
string isenabled = radiolistStatus.SelectedValue.ToString();
cmd.Connection = conn;
cmd.CommandText = "UPDATE NatureOfWork SET NatureOfWorkname ='" + txtNatureOfWorkName.Text + "',IsNowEnabled=" + isenabled.ToString() + " WHERE NatureOFWorkID=" + lblNautureOfWorkID.Text.ToString();
conn.Open();
cmd.ExecuteNonQuery();
GVNatureOFWork.EditIndex = -1;
BindGrid();
conn.Close();
}
protected void GVNatureOFWork_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblNautureOfWorkID = (Label)e.Row.FindControl("lblNautureOfWorkID");
Label lblNatureOfWorkName = (Label)e.Row.FindControl("lblNatureOfWorkName");
TextBox txtNatureOfWorkName = (TextBox)e.Row.FindControl("txtNatureOfWorkName");
TextBox txtAddNatureOfWorkName = (TextBox)e.Row.FindControl("txtAddNatureOfWorkName");
RadioButtonList radiolistStatusAdd = (RadioButtonList)e.Row.FindControl("radiolistStatusAdd");
RadioButtonList radiolistStatus = (RadioButtonList)e.Row.FindControl("radiolistStatus");
Label lblNOWStatus = (Label)e.Row.FindControl("lblNOWStatus");
}
if (e.Row.RowType == DataControlRowType.Footer)
{
}
}
protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
{
string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
BindRegionProjectInfoGrid();
}