方法一:是在模板中直接嵌套,指定其后台处理方法,在后台方法中通过寻找其parent 处理相关数据。
后台代码:
方法二:指定Button中的Command为Select 在GridView的控件Select事件处理方法中处理数据。
Code
<asp:GridView ID="grvwList" CssClass ="GridViewStyle" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnRowDataBound="grvwList_RowDataBound" OnPageIndexChanging="grvwList_PageIndexChanging">
<Columns>
<asp:BoundField DataField="fld_Name" HeaderText="服务名称">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="fld_PointPerUnit" HeaderText="计费点数" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="fld_Cycle" HeaderText="计费周期" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="fld_Unit" HeaderText="计费单位" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="fld_Remark" HeaderText="备注" >
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:TemplateField HeaderText="订制服务">
<ItemTemplate><asp:CheckBox ID="cBoxCheck" runat="server" AutoPostBack="true" OnCheckedChanged="cBoxCheckHandler" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="labHidden" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "existid")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">查看详情</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="GridViewHeaderStyle " />
<RowStyle Height="20px" CssClass="GridViewRowStyle " />
<%-- <SelectedRowStyle CssClass="GridViewSelectedRowStyle " />--%>
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle " />
<FooterStyle CssClass="GridViewFooterStyle " />
<PagerStyle CssClass="GridViewPagerStyle " />
</asp:GridView>
<asp:GridView ID="grvwList" CssClass ="GridViewStyle" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnRowDataBound="grvwList_RowDataBound" OnPageIndexChanging="grvwList_PageIndexChanging">
<Columns>
<asp:BoundField DataField="fld_Name" HeaderText="服务名称">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="fld_PointPerUnit" HeaderText="计费点数" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="fld_Cycle" HeaderText="计费周期" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="fld_Unit" HeaderText="计费单位" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="fld_Remark" HeaderText="备注" >
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:TemplateField HeaderText="订制服务">
<ItemTemplate><asp:CheckBox ID="cBoxCheck" runat="server" AutoPostBack="true" OnCheckedChanged="cBoxCheckHandler" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="labHidden" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "existid")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">查看详情</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="GridViewHeaderStyle " />
<RowStyle Height="20px" CssClass="GridViewRowStyle " />
<%-- <SelectedRowStyle CssClass="GridViewSelectedRowStyle " />--%>
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle " />
<FooterStyle CssClass="GridViewFooterStyle " />
<PagerStyle CssClass="GridViewPagerStyle " />
</asp:GridView>
后台代码:
Code
protected void cBoxCheckHandler(object sender, EventArgs e)
{
CheckBox check = (CheckBox)sender;
GridViewRow gRow = check.Parent.Parent as GridViewRow;
SqlConnection con = new SqlConnection(ConnString.GetCrunodeConnStr());
SqlParameter[] _para ={
new SqlParameter("@fld_CpyID", DbType.Int32),
new SqlParameter("@fld_SrvName",DbType.String),
};
_para[0].Value = iCompanyId;
_para[1].Value = gRow.Cells[0].Text;
string strCmd="";
if (check.Checked)
{
strCmd = "insert into T_subsServ (fld_CpyID,fld_SrvName) values (@fld_CpyID,@fld_SrvName)";
}
else
{
strCmd = "delete from T_subsServ where fld_CpyID=@fld_CpyID and fld_SrvName=@fld_SrvName ";
}
int intReturn = SqlHelper.ExecuteNonQuery(con, CommandType.Text,strCmd, _para);
}
protected void cBoxCheckHandler(object sender, EventArgs e)
{
CheckBox check = (CheckBox)sender;
GridViewRow gRow = check.Parent.Parent as GridViewRow;
SqlConnection con = new SqlConnection(ConnString.GetCrunodeConnStr());
SqlParameter[] _para ={
new SqlParameter("@fld_CpyID", DbType.Int32),
new SqlParameter("@fld_SrvName",DbType.String),
};
_para[0].Value = iCompanyId;
_para[1].Value = gRow.Cells[0].Text;
string strCmd="";
if (check.Checked)
{
strCmd = "insert into T_subsServ (fld_CpyID,fld_SrvName) values (@fld_CpyID,@fld_SrvName)";
}
else
{
strCmd = "delete from T_subsServ where fld_CpyID=@fld_CpyID and fld_SrvName=@fld_SrvName ";
}
int intReturn = SqlHelper.ExecuteNonQuery(con, CommandType.Text,strCmd, _para);
}
方法二:指定Button中的Command为Select 在GridView的控件Select事件处理方法中处理数据。
Code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="18" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowDeleting="GridView1_RowDeleting"
Height="304px" Width="580px">
<Columns>
<asp:TemplateField HeaderText="网址">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("fld_url") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" Value='<%# Bind("fld_urlId") %>' runat="server" />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("fld_url") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("fld_Audited") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("fld_Audited") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="备注">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("fld_comment") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("fld_comment") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="公司名称" Visible="false">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
Text="审核" Width="50px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" Width="50px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码:<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="18" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowDeleting="GridView1_RowDeleting"
Height="304px" Width="580px">
<Columns>
<asp:TemplateField HeaderText="网址">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("fld_url") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" Value='<%# Bind("fld_urlId") %>' runat="server" />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("fld_url") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("fld_Audited") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("fld_Audited") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="备注">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("fld_comment") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("fld_comment") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="公司名称" Visible="false">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
Text="审核" Width="50px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除" Width="50px"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string strState = ((Label)GridView1.Rows[e.NewSelectedIndex].FindControl("Label3")).Text;
int id = int.Parse(((HiddenField)GridView1.Rows[e.NewSelectedIndex].FindControl("HiddenField1")).Value);
if (strState == "待批复")
{
SqlConnection conn = DbConnection.ConnectSqlDb();
SqlCommand cmd = new SqlCommand("update t_urlList set fld_Audited=1 where fld_urlId="+id+" ", conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
UrlBind();
}
catch
{
return;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string strState = ((Label)GridView1.Rows[e.NewSelectedIndex].FindControl("Label3")).Text;
int id = int.Parse(((HiddenField)GridView1.Rows[e.NewSelectedIndex].FindControl("HiddenField1")).Value);
if (strState == "待批复")
{
SqlConnection conn = DbConnection.ConnectSqlDb();
SqlCommand cmd = new SqlCommand("update t_urlList set fld_Audited=1 where fld_urlId="+id+" ", conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
UrlBind();
}
catch
{
return;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}