setting radiobutton selected value on row edit

Last post 08-29-2008 1:06 PM by Donetcoder08. 6 replies.

Sort Posts:

  • setting radiobutton selected value on row edit

    08-25-2008, 10:50 AM
    • Member
      4 point Member
    • Donetcoder08
    • Member since 03-18-2008, 1:03 PM
    • Birmingham
    • Posts 49

    I have a gridview with a radiobutton list.  On databound, I pull the data from the database and select the correct value in the radiobutton list. but if it changes, I place the row in edit mode because they may have to enter more information.  The problem I am having right now is the radiobutton is losing its selected value.  Here is the code.  Any suggestions would be welcome.

    Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    ' get sender (the RadioButtonList)

    Dim RBL As RadioButtonList = TryCast(sender, RadioButtonList)

    ' it's container is a GridViewRow

    Dim GVR As GridViewRow = TryCast(RBL.NamingContainer, GridViewRow)

    'Dim GVR As GridViewRow = GridView1.Rows(e.RowIndex)

    ' put the row into edit mode

    GridView1.EditIndex = GVR.RowIndex

    Dim status As String = DirectCast(GridView1.Rows(GVR.RowIndex).Cells(2).Controls(1), RadioButtonList).SelectedValue

    CType(GridView1.Rows(GVR.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedValue = status

    End Sub

  • Re: setting radiobutton selected value on row edit

    08-25-2008, 10:55 AM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs

    If you are losing state on your RadioButtonList, then it may be that you are data-binding your GridView to its DataSource each and every postback instead of just on the initial Page load. If so, make sure to data-bind your GridView within a Not IsPostBack condition.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: setting radiobutton selected value on row edit

    08-25-2008, 11:14 AM
    • Member
      4 point Member
    • Donetcoder08
    • Member since 03-18-2008, 1:03 PM
    • Birmingham
    • Posts 49

    I don't know if I can do that since I the gridview doesn't show up until postback.  Is there anything else on the gridview databound event?

    I tried this and it didn't work.

     

    For Each gvRow As GridViewRow In GridView1.Rows

    If Not GridView1.EditIndex = gvRow.RowIndex Then

  • Re: setting radiobutton selected value on row edit

    08-25-2008, 11:18 AM
    Answer
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs

    If your GridView is going to be data-bound each and every postback, then its almost impossible to expect to maintain state. If you don't want your GridView to appear until a certain Button is pressed, then bind your GridView to its data within the Button's Click event handler.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: setting radiobutton selected value on row edit

    08-28-2008, 11:10 AM
    • Member
      4 point Member
    • Donetcoder08
    • Member since 03-18-2008, 1:03 PM
    • Birmingham
    • Posts 49

    OK, I created another version of this same page, it just has a button click event and the event when you click on the radio button.  I can reference the value of the radio button clicked and the row index but I can't get that row to update the selected value of that radio button.  How do reference the radiobuttonlist on the correct row so I can set it's selected value?

    GridView1.EditIndex = GVR.RowIndex

    Session(
    "status") = status

    If Session("status") Is Nothing Then

    Else

    Response.Write(Session("status"))

    End If

    Response.Write(GVR.RowIndex)

    RBL =
    CType(GVR.FindControl("RadioButtonList1"), RadioButtonList)

    CType(GVR.FindControl("RadioButtonList1"), RadioButtonList).SelectedValue = status

  • Re: setting radiobutton selected value on row edit

    08-29-2008, 4:32 AM
    Answer

    Hi Donetcoder08,

    I think your description is not so clear, and I can not fully understand what you really want to do.

    Based on my understanding , you can to convert that row to editmode and find the radiobuttonlist in edittemplate and restore it's selectedvalue to the value stored in session.

    You code above may be wrong. Because you only set GridView1.EditIndex = GVR.RowIndex and this is not enough.

    Usually, you need to do some steps below to make that row to edit mode

    • set editindex just like GridView1.EditIndex = GVR.RowIndex
    • set gridview's datasource (or you connect the gridview to one datasource control , you can omit this step)
    • invoke gridview's DataBind method.

     

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: setting radiobutton selected value on row edit

    08-29-2008, 1:06 PM
    • Member
      4 point Member
    • Donetcoder08
    • Member since 03-18-2008, 1:03 PM
    • Birmingham
    • Posts 49

    Here is the entire code.  I think I am doing all that you ask.

    Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim strSelectCommand As String

    strSelectCommand = "SELECT a.statusnotes,a.assetid, a.sessionid, v.VendorName,a.statuscode, a.Amount, a.KeyedDate, a.InvoiceNumber FROM OpsandTechInvoiceManagement_Invoices a INNER JOIN OpsandTechInvoiceManagement_Vendors v ON a.VendorID = v.VendorID "

    If txtInvoiceNum.Text <> "" Then

    strSelectCommand = strSelectCommand & "and InvoiceNumber ='" & txtInvoiceNum.Text & "'"

    End If

    If txtjobid.Text <> "" Then

    strSelectCommand = strSelectCommand & "and sessionid ='" & txtjobid.Text & "'"

    End If

    strSelectCommand = strSelectCommand & "and Statuscode <> 'A'"

    strSelectCommand = strSelectCommand & "and invoicetype <> 'TR'"

    strSelectCommand = strSelectCommand & " order by keyedDate desc"

    ' get sender (the RadioButtonList)

    Dim RBL As RadioButtonList = TryCast(sender, RadioButtonList)

    ' it's container is a GridViewRow

    Dim GVR As GridViewRow = TryCast(RBL.NamingContainer, GridViewRow)

    ' put the row into edit mode

    GridView1.EditIndex = GVR.RowIndex

    Dim status As String = DirectCast(GridView1.Rows(GVR.RowIndex).Cells(2).Controls(1), RadioButtonList).SelectedValue

    Session("status") = status

     

    GridView1.DataBind()

     

    End Sub

Page 1 of 1 (7 items)