急!gridview加RadioButton
- HTML code
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataKeyNames="ID" DataSourceID="AccessDataSource1" GridLines="None"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="pyear" HeaderText="计划年度" SortExpression="pyear" /> <asp:BoundField DataField="pnumber" HeaderText="计划编号" SortExpression="pnumber" /> <asp:BoundField DataField="ptype" HeaderText="计划类型" SortExpression="ptype" /> <asp:BoundField DataField="punit" HeaderText="申报单位" SortExpression="punit" /> <asp:BoundField DataField="pwman" HeaderText="填报人" SortExpression="pwman" /> <asp:BoundField DataField="pphone" HeaderText="联系电话" SortExpression="pphone" /> <asp:BoundField DataField="pwdate" HeaderText="填报日期" SortExpression="pwdate" /> <asp:BoundField DataField="pstate" HeaderText="审核状态" SortExpression="pstate" /> <asp:BoundField DataField="premark" HeaderText="备注" SortExpression="premark" /> <asp:TemplateField HeaderText="审核"> <ItemTemplate> <asp:RadioButton ID="yesBtn" runat="server" Text="通过" GroupName="shenhe" AutoPostBack="True" /> <asp:RadioButton ID="noBtn" runat="server" Text="不通过" GroupName="shenhe" AutoPostBack="True" /> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#C6C3C6" ForeColor="Black" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" /> <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" /> <RowStyle BackColor="#DEDFDE" ForeColor="Black" /> <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#594B9C" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#33276A" /> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/fac.accdb" SelectCommand="SELECT * FROM [fpbasic]"> </asp:AccessDataSource> </div> <div> <asp:Button ID="ButSubmit" runat="server" Text="提交" /> </div>
这样应该怎么写后台代码,当我点提交的时候使RadioButton的值存入到数据库中(因为有很多行,每一行的RadioButton都会有一个选项,是一次性提交,不是每选一次RadioButton就与服务器交互一次)
------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
RadioButton yesBtn = row.FindControl("yesBtn") as RadioButton ;
RadioButton noBtn = row.FindControl("noBtn") as RadioButton;
if (yesBtn.Checked)
{
sql 语句update
}
if (noBtn.Checked)
{
sql 语句update
}
}
}
[解决办法]
你最好使用RadioButtonList啊,直接到得到,更新数据库
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
RadioButtonList x= row.FindControl("x") as RadioButtonList ;
sql = "update x set xx = " + x.SelectedValue;
}
}
[解决办法]
前面不是给你提示了吗?
另外
你需要注意
1, <asp:ListItem>通过</asp:ListItem>内容是否与数据库中一致
2,是否需要先绑定数据库中的原有设置
3,还需要注意2个都不选的情况