How to update the Status column when a radio button is checked?
The Status column may have values 0,3,5, or 8. The initial display of rows in the gridview shows the appropriate radio button checked if the value is 3,5, or 8 and none are checked if the Status value is 0.
I want to update the status value for the row when one of the radio buttons is checked.
As it stands now, checking a button causes a postback, but the database is not updated.
In the aspx file
In the aspx.cs file
The Status column may have values 0,3,5, or 8. The initial display of rows in the gridview shows the appropriate radio button checked if the value is 3,5, or 8 and none are checked if the Status value is 0.
I want to update the status value for the row when one of the radio buttons is checked.
As it stands now, checking a button causes a postback, but the database is not updated.
In the aspx file
Code:
...
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString=
"<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [UANC_id], [UserName], [Status]
FROM [Alternatives]"
ConflictDetection="OverwriteChanges"
OldValuesParameterFormatString="original_{0}"
UpdateCommand="UPDATE [Alternative] SET
[Status] = @Status
WHERE [UANC_id] = @original_UANC_id">
<UpdateParameters>
<asp:Parameter Name="Status" Type="Int32" />
<asp:Parameter Name="UANC_id" Type="Int32" />
<asp:Parameter Name="original_UANC_id" Type="Int32" />
<asp:Parameter Name="original_Status" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" CellPadding="4" AutoGenerateColumns="False" DataKeyNames="UANC_id" >
<Columns>
<asp:BoundField DataField="UserName"
HeaderText="UserName" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server"
GroupName="Status" Text="Closed"
Value="8"
Checked='<%# (int)Eval("Status") == 8 %>'
AutoPostBack="true"
OnCheckedChanged="RadioButton1_CheckedChanged"
/>
<asp:RadioButton ID="RadioButton2" runat="server"
GroupName="Status" Text="Changed"
Value="5"
Checked='<%# (int)Eval("Status") == 5 %>'
AutoPostBack="true"
OnCheckedChanged="RadioButton2_CheckedChanged"
/>
<asp:RadioButton ID="RadioButton3" runat="server"
GroupName="Status" Text="Blocked"
Value="3"
Checked='<%# (int)Eval("Status") == 3 %>'
AutoPostBack="true"
OnCheckedChanged="RadioButton3_CheckedChanged"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
...
In the aspx.cs file
Code:
...
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
SqlDataSource1.Update();
GridView1.DataBind();
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
SqlDataSource1.Update();
GridView1.DataBind();
}
protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
{
SqlDataSource1.Update();
GridView1.DataBind();
}
...