Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hye..

how do i keep a HTML input(radio) value after autopostback?
This is how my system flows.
I have a dropdownlist (autopostback = true) and will display the data in gridview according to that dropdownlist value.
In the gridview, there are HTML input(radio) where the users must choose only one option. Then, they must click on Choose button to cause second gridview appears.
But how do i want to keep the input(radio) selected after the user click on Choose button?
this is my piece of code. the input radio that i want to keep selected is named as gvradio.

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
                    CellPadding="3" DataKeyNames="IdInstitusi"
                    DataSourceID="SqlDataSourceAlamatInst" Width="398px" Visible="False">
                    <RowStyle ForeColor="#000066" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <input   name="gvradio" type="radio" enableviewstate="true"  value='<%#Eval("IdInstitusi")%>' />

                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="AlamatInstitusi" HeaderText="AlamatInstitusi"
                            SortExpression="AlamatInstitusi" />

</asp:GridView>
Posted

The problem seems to be you are loading the GridView everytime the page postback.
Make sure you load the GridView only once. Add a condition like below,

C#
if (!IsPostBack)
{
// Load the GridView
}


Mark it as answer if it is helpful
 
Share this answer
 
Comments
snamyna 11-Oct-11 0:10am    
i have changed the code a little bit where i put Gridview1, Choose button and Gridview 2 in a single Update Panel. Once i click the button, it does not autopostback, it still retrieve the value in Gridview 2 according to Gridview 1 selection, but the selection in Gridview1 is still missing. Any idea?
Venkatesh Mookkan 11-Oct-11 0:15am    
Can you update the question with your code? It would help us to understand your problem better
snamyna 11-Oct-11 0:21am    
Sorry.. The elements involved here are Dropdownlist (NamaInstitusiDDL), Gridview1, ChooseBtn, and Gridview2 which are located in one Update Panel (UpdatePanel3)

<asp:UpdatePanel ID="UpdatePanel3" runat="server"><contenttemplate>
<asp:DropDownList ID="NamaInstitusiDDL" runat="server" AutoPostBack="True" AppendDataBoundItems="true"

DataSourceID="SQLNamaInstitusi" DataTextField="NamaInstitusi"
DataValueField="NamaInstitusi" Width="395px" CausesValidation="True"> <asp:ListItem Text="--Sila pilih institusi--" Value="" /> <br /><br />
<asp:Panel ID="Panel1" runat="server" Height="327px" ScrollBars="Both"
Width="413px"><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="IdInstitusi"
DataSourceID="SqlDataSourceAlamatInst" Width="398px" Visible="False">
<rowstyle forecolor="#000066">
<columns>
<asp:TemplateField>
<itemtemplate>
<input name="gvradio" type="radio" enableviewstate="true" value='<%#Eval("IdInstitusi")%>' />


<itemstyle horizontalalign="Center" verticalalign="Middle">

<asp:BoundField DataField="AlamatInstitusi" HeaderText="AlamatInstitusi"
SortExpression="AlamatInstitusi" />
<asp:BoundField DataField="IdInstitusi" HeaderText="IdInstitusi"
ReadOnly="True" SortExpression="IdInstitusi" Visible="False" />

<footerstyle backcolor="White" forecolor="#000066">
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlDataSource1"
EmptyDataText="Tiada rekod. Sila Tambah Penyelaras Baru" Visible="False"
Width="398px">
<columns>
<asp:TemplateField>
<itemtemplate>
<input name="penyelarasradio" type="radio"
value='<%#Eval("IdPenyelarasIPT")%>' />

<itemstyle horizontalalign="Center" verticalalign="Middle">

<asp:BoundField DataField="NamaPenyelaras" HeaderText="NamaPenyelaras"
SortExpression="NamaPenyelaras" />

<emptydatarowstyle font-bold="True">
<footerstyle backcolor="White" forecolor="#000066">
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<rowstyle forecolor="#000066">
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

<br />
add Runat="Server" attribute.

ASP.NET
<input   name="gvradio" type="radio" enableviewstate="true" runat="server" value='<%#Eval("IdInstitusi")%>' />
 
Share this answer
 
v2
Comments
snamyna 11-Oct-11 0:25am    
Sorry but once i put runat= server, the user can select multiple choices which I don't want that.Anyway, thanks for your time. :)
koolprasad2003 11-Oct-11 0:35am    
yeah, we can trigger that behavior by writting javascript. so that you can select only one radio at a time
Try Asp RadioList or radio control to solve this problem.
 
Share this answer
 
at the load event
put this code

if(!IsPostback)
{
//your code
}
 
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