Click here to Skip to main content
15,894,105 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to do a survey. In my project , I have a problem. That is;
RadioButtonList AND ListItem in gridview dosent work in GridView1_PageIndexChanging sub; when I click a
"next button" on the bottom of the page, it is not retaining the
users selection. It either retains -1 (no selection), or whatever I
set it to during the GridView1_PageIndexChanging event.

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                CellPadding="4" ForeColor="#333333" GridLines="None" Height="118px" 
                                Width="563px" style="margin-top: 0px" 
                                onpageindexchanging="GridView1_PageIndexChanging" 
                                onrowdatabound="GridView1_RowDataBound1" 
                                AllowPaging="True" onpageindexchanged="GridView1_PageIndexChanged">
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                <PagerSettings  Mode="NextPreviousFirstLast" FirstPageText="İlk" PreviousPageText="Geri" NextPageText="İleri" LastPageText="Son" />

                                <Columns>

                             <asp:TemplateField>

                                        <ItemTemplate>
                                            <%# Container.DataItemIndex + 1 %>
                                        </ItemTemplate>
                                         <ItemStyle Width="5px" />
                                    </asp:TemplateField>

                             <asp:TemplateField HeaderText="Soru" SortExpression="Soru">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Soru") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label14" runat="server" Text='<%# Bind("Soru") %>'></asp:Label>
                                        </ItemTemplate>

                                        <HeaderStyle HorizontalAlign="Justify" />
                                        <ItemStyle Width="200px" />
                                    </asp:TemplateField>
                                  
                             <asp:TemplateField HeaderText="column1" SortExpression="column1" Visible="False">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("column1") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label19" runat="server" Text='<%# Bind("column1") %>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Justify" />
                                        <ItemStyle Width="8px" />
                                    </asp:TemplateField>
                                                
                             <asp:TemplateField>
                   <ItemTemplate>
                         <asp:RadioButtonList ID="rblChoices" runat="server" RepeatDirection="Horizontal"   >
                            <asp:ListItem Value="1"></asp:ListItem>  
                            <asp:ListItem Value="2"></asp:ListItem> 
                            <asp:ListItem Value="3"></asp:ListItem> 
                            <asp:ListItem Value="4"></asp:ListItem> 
                            <asp:ListItem Value="5"></asp:ListItem> 

                        </asp:RadioButtonList>
                    </ItemTemplate>  
                     <ItemStyle Width="130px" />
                    </asp:TemplateField>

               
                    <asp:TemplateField HeaderText="Anket_ID" SortExpression="Anket_ID" Visible="False">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Anket_ID") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label16" runat="server" Text='<%# Bind("Anket_ID") %>'></asp:Label>
                                        </ItemTemplate>

                                        <HeaderStyle HorizontalAlign="Justify" />
                                        <ItemStyle Width="200px" />
                                    </asp:TemplateField>
                 
                   
                    <asp:TemplateField HeaderText="User_ID" SortExpression="User_ID" Visible="False">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("User_ID") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label17" runat="server" Text='<%# Bind("User_ID") %>'></asp:Label>
                                        </ItemTemplate>

                                        <HeaderStyle HorizontalAlign="Justify" />
                                        <ItemStyle Width="200px" />
                                    </asp:TemplateField>
                 
                

                   <asp:TemplateField HeaderText="UygSoru_ID" SortExpression="UygSoru_ID" Visible="False">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("UygSoru_ID") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label18" runat="server" Text='<%# Bind("UygSoru_ID") %>'></asp:Label>
                                        </ItemTemplate>

                                        <HeaderStyle HorizontalAlign="Justify" />
                                        <ItemStyle Width="200px" />
                                    </asp:TemplateField>
                 
                                </Columns>
                                <EditRowStyle BackColor="#999999" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            </asp:GridView>



and in c#

C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataBind();
     /*  for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           RadioButtonList c = (RadioButtonList)GridView1.Rows[i].Cells[3].FindControl("rblChoices");
           Label ettiket = (Label)GridView1.Rows[i].Cells[2].FindControl("Label19");
         //   Label ettiket = (Label)GridView1.Rows[i].Cells[2].FindControl("Label19");


           switch (ettiket.Text)
           {
               case "1": c.SelectedIndex = 0; break;
               case "2": c.SelectedIndex = 1; break;
               case "3": c.SelectedIndex = 2; break;
               case "4": c.SelectedIndex = 3;break;
               case "5": c.SelectedIndex = 4; break;
           }
           */
           for (int i = 0; i < GridView1.Rows.Count; i++)
           {
               RadioButtonList c = (RadioButtonList)GridView1.Rows[i].Cells[3].FindControl("rblChoices");

               Label anketid = (Label)GridView1.Rows[i].Cells[4].FindControl("Label16");
               Label userid = (Label)GridView1.Rows[i].Cells[5].FindControl("Label17");
               Label soruid = (Label)GridView1.Rows[i].Cells[6].FindControl("Label18");
               if (c.SelectedIndex > -1)
               {
                   Label20.Text = "You selected: " + c.SelectedItem.Text;
                   //  BusinessLayer.UserAnketKaydet(Convert.ToInt16(anketid.Text), Convert.ToInt16(soruid.Text), 1, Convert.ToInt16(userid.Text));

               }
           }
       }



then ; when I clıck "next" or "back" button ; ı dont see selectedvalues;
in fact
C#
Label20.Text = "You selected: " + c.SelectedItem.Text;

that must work but it doesnt work??

What is the problem;

Thanksss
Posted

First

ASP.NET
 <asp:radiobuttonlist id="rblChoices" runat="server" repeatdirection="Horizontal" onselectedindexchanged="rblCategories_SelectedIndexChanged" autopostback="True" xmlns:asp="#unknown">
</asp:radiobuttonlist>


then

C#
protected void rblCategories_SelectedIndexChanged(object sender, EventArgs e)
    {

        var listControl = (ListControl)sender;
        var row = (GridViewRow)listControl.NamingContainer;
        var item = listControl.SelectedItem;
        Label anketid = (Label)row.FindControl("Label16");
        Label userid = (Label)row.FindControl("Label17");
        Label soruid = (Label)row.FindControl("Label18");
        try
        {  label20.Text= item.Text  /Radio Button Selected Value
               
          //  BusinessLayer.UserAnketKaydet(Convert.ToInt16(anketid.Text), Convert.ToInt16(soruid.Text), Convert.ToInt16(item.Text), Convert.ToInt16(userid.Text));
        }
        catch
        {
        }
 
Share this answer
 
I think this is the answer:
http://stackoverflow.com/questions/6412025/gridview-paging-not-working[^]

I quote:

Your binding order is not correct.. It should be like...
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)     
{

 GridView1.PageIndex = e.NewPageIndex;
 GridView1.SelectedIndex = -1;
 BindGrid(); // Call bind here
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900