Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
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: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#
 
 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
 
 Label20.Text = "You selected: " + c.SelectedItem.Text;
that must work but it doesnt work??
 
What is the problem;
 
Thanksss
Posted 5-Dec-12 11:37am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
} 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

First
 
 <asp:radiobuttonlist id="rblChoices" runat="server" repeatdirection="Horizontal" onselectedindexchanged="rblCategories_SelectedIndexChanged" autopostback="True" xmlns:asp="#unknown">
</asp:radiobuttonlist>
 
then
 
 
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
        {
        }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,745
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,599
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100