Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UPDATE using an ItemTemplate

Status
Not open for further replies.

rac2

Programmer
Apr 26, 2001
1,871
US
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
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"
         />&nbsp;&nbsp;
         <asp:RadioButton ID="RadioButton2" runat="server" 
              GroupName="Status" Text="Changed" 
              Value="5" 
              Checked='<%# (int)Eval("Status") == 5 %>' 
              AutoPostBack="true" 
              OnCheckedChanged="RadioButton2_CheckedChanged"
          />&nbsp;&nbsp;
          <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();
    }
...
 
Using the built-in drag and drop data controls make debugging very difficult. However, if you must use them then I'd suggest starting by using SQL Profiler to see exactly what commands are being sent to your database.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
When I check one of the radio buttons, SQL Profiler records this TextData.
Code:
UPDATE [Alternative] SET [Status] = @Status WHERE [UANC_id] = @original_UANC_id

I would think that the parameters should have been replaced by values before SQL Server saw the query.

And that those values would be provided by the event method for the radio button that was checked.

If so, how to do that? Or, where else to look.
 
But then I see the same query with parameters, not values, with a different approach that successfully updates the database. This approach uses the straightforward gridview with an edit command column.

So I think the TextData from the SQL Profiler trace does not show the values, only the query with parameters.

 
SQL Profiler shows exactly what commands are being executed. If the values are not being passed, e.g. you don't see something like:
Code:
[blue]exec[/blue] mystoredprocedure @myparameter=[red]'test'[/red]
then the update cannot work. Unfortunately the bound controls make anything like this far too hard to debug and it's why I think they are best avoided.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top