gridview databind only works sometimes

R

rlm

I have standard gridview in an updatePanel with an AJAX timer that refreshes
the data every 5 seconds by calling the grids DataBind method in the tick
event. This works great.

However, I have a checkbox on the web page which when checked I want to hide
some rows. The code that does the hiding is in the RowDataBound event where I
do a bunch of other stuff. This way every time the data is updated the rows
are correctly displayed or hidden. And that code works on the 5 second
updates, but when they click the checkbox I don't want them to have to wait 5
seconds. But in the server side click event of the check box when it call the
grid's DataBind() event, it either doesn't work, or at least it never
executes my RowDataBound event code like it does when the time calls it.

Any ideas why DataBind works from the Timer_Tick and not the checkbox click?
 
M

Milosz Skalecki [MCAD]

Hi there,

It should look similar to the following code:

-- begin aspx code --
<asp:ScriptManager runat="server" ID="scriptManager" />
<asp:UpdatePanel runat="server" ID="updatePanel" UpdateMode="Conditional"
ChildrenAsTriggers="true">
<ContentTemplate>
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# DateTime.Now.Ticks %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />
<asp:AsyncPostBackTrigger ControlID="checkBox" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer runat="server" ID="timer" OnTick="timer_Tick" Enabled="true"
Interval="5000" />
<asp:CheckBox runat="server" ID="checkBox" Text="Option" AutoPostBack="True"
OnCheckedChanged="checkBox_CheckedChanged" />
-- end aspx code --

-- begin c# code beside --

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

private void BindData()
{
// use the checkbox's value
gv.DataSource = new int[5];
gv.DataBind();
}
protected void checkBox_CheckedChanged(object sender, EventArgs e)
{
BindData();
}
protected void timer_Tick(object sender, EventArgs e)
{
BindData();
}
-- end c# code beside --

hope it helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top