I have templatefields that contain buttons in my gridview which is inside an update panel. Following is the aspx definition
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="10000">
</asp:Timer>
<div id="grid-view-container2">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" OnRowCommand="GridView2_RowCommand" OnRowUpdating="GridView2_RowUpdating">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:templatefield HeaderStyle-HorizontalAlign="Center" headertext="Time">
<ItemStyle HorizontalAlign="Center" />
<itemtemplate>
<asp:Label ID="lbltime" runat="server" Text='<%#Eval("Time") %>'></asp:Label>
</itemtemplate>
</asp:templatefield>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="button1" runat="server" CausesValidation="false" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="show" Font-Bold="true" EnableViewState="true" OnClick="button1_click" Text="Hide" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:Button ID="button2" runat="server" CausesValidation="false" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="update" Enabled="false" EnableViewState="true" Font-Bold="true" OnClick="button2_click" Text="Update" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
in my code behind file i am changing text of buttons when clicked as follows
protected void button1_click(object sender, EventArgs e)
{
flag = 0;
}
protected void button2_click(object sender, EventArgs e)
{
flag = 1;
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (flag == 0)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView2.Rows[index];
Button b1 = (Button)row.Cells[1].FindControl("button1");
Button b2 = (Button)row.Cells[2].FindControl("button2");
if (b1.Text == "Show")
{
b2.Enabled = true;
b1.Text = "Hide";
Label lb = (Label)row.Cells[3].FindControl("lblid");
int ID = int.Parse(lb.Text);
foreach (GridViewRow gr in GridView1.Rows)
{
Label lb1 = (Label)gr.Cells[0].FindControl("lblid1");
int id1 = int.Parse(lb1.Text);
if (ID == id1)
{
gr.Visible = true;
}
}
}
else
{
b1.Text = "Show";
b2.Enabled = false;
Label lb2 = (Label)row.Cells[3].FindControl("lblid");
int id2 = int.Parse(lb2.Text);
foreach (GridViewRow gr1 in GridView1.Rows)
{
Label lb3 = (Label)gr1.Cells[0].FindControl("lblid1");
int id3 = int.Parse(lb3.Text);
if (id2 == id3)
{
gr1.Visible = false;
}
}
}
}
if (flag == 1)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView2.Rows[index];
Button b2 = (Button)row.Cells[2].FindControl("button2");
Button b1 = (Button)row.Cells[1].FindControl("button1");
if(b2.Text=="Update")
{
Label lb4 = (Label)row.Cells[3].FindControl("lblid");
int ID4 = int.Parse(lb4.Text);
foreach (GridViewRow gr in GridView1.Rows)
{
Label lb5 = (Label)gr.Cells[0].FindControl("lblid1");
int id5 = int.Parse(lb5.Text);
if (ID4 == id5)
{
TextBox tb = (TextBox)gr.Cells[5].FindControl("textbox1");
tb.ReadOnly = false;
}
}
b2.Text = "Submit";
}
else
{
b2.Text = "Update";
Label lb = (Label)row.Cells[3].FindControl("lblid");
int ID = int.Parse(lb.Text);
float Fill_Price;
foreach (GridViewRow gr in GridView1.Rows)
{
Label lb3 = (Label)gr.Cells[0].FindControl("lblid1");
int id3 = int.Parse(lb3.Text);
if (ID == id3)
{
TextBox tb = (TextBox)gr.Cells[5].FindControl("textbox1");
try
{
Fill_Price = float.Parse(tb.Text);
Update_tab(ID, Fill_Price);
getdata1();
}
catch
{
Response.Redirect("datapage.aspx");
}
}
}
}
}
}
i have two functions where i bind my grid one for when page is !postback and other when timer's tick occurs
public void getdata2()//for gridview 2
{
con.Open();
SqlCommand cmmd = new SqlCommand("select Time, ID from mytable where Date=(select MAX(Date) from mytable ) order by Time asc ", con);
SqlDataAdapter da2 = new SqlDataAdapter(cmmd);
da2.Fill(ds2, "mytable");
GridView2.DataSource = ds2.Tables["mytable"];
GridView2.DataBind();
ViewState["myvs1"] = GridView2.DataSource;
SqlCommand cmmd2 = new SqlCommand("SELECT MAX(ID) FROM mytable;", con);
a = (Int32)cmmd2.ExecuteScalar();
ViewState["myvs2"] = a;
con.Close();
}
public void onrefresh_gv2()
{
try
{
con.Open();
int a = (int)ViewState["myvs2"];
SqlCommand cmmd = new SqlCommand("select Time, ID from mytable where ID >"+ a + "order by Time asc", con);
SqlDataAdapter dta = new SqlDataAdapter(cmmd);
dta.Fill(ds2, "mytable2");
DataTable d2=ds2.Tables["mytable2"];
mydt = (DataTable)ViewState["myvs1"];
d2.Merge(mydt);//ds2.Tables["mytable2"].Merge(mydt);
GridView2.DataSource = d2;//ds2.Tables["mytable2"];
GridView2.DataBind();
SqlCommand cmmd2 = new SqlCommand("SELECT MAX(ID) FROM mytable;", con);
a = (Int32)cmmd2.ExecuteScalar();
ViewState["myvs1"] = GridView2.DataSource;
ViewState["myvs2"] = a;
}
catch
{
Response.Redirect("datapage.aspx");
}
finally
{
con.Close();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
onrefresh_gv2();
}
the problem is that the text of button is not retained on partial postback ( ie on click the text of button1 changes from hide to show which is not retained ) and all buttons come to their initial state when partial postback occurs.
how can i fulfill above task?
Thanks.
Joy