I'm building a table of data for price quotes (think table of Stock quotes) which needs to be refreshed every 5 secs. Each row has some data about one Stock in several columns and the last column in each row has a LinkButton to see more info about that particular stock. Everything works but the LinkButton. The entire table is nested inside an UpdatePanel which I think is causing the problem. I've seen a fair number of posts on this topic but none that have worked for me.
Here is my .aspx code:
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:Timer ID="Timer" OnTick="Timer_Tick" runat="server" Interval="5000" />
<div id="itemList">
<asp:UpdatePanel ID="itemPanel" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
<Triggers><asp:AsyncPostBackTrigger ControlID="Timer" /></Triggers>
<ContentTemplate>
<asp:Panel ID="Panel_ItemList" runat="server" width="100%"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
and my .aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
...
if (!Page.IsPostBack)
{
updateItemsTable();
}
}
protected void LinkButton_Click(object sender, CommandEventArgs e)
{
Panel_LoginAlert.Visible = true; // <-- THIS IS NOT FIRING!!
}
protected void Timer_Tick(object sender, EventArgs e)
{
updateItemsTable();
}
protected void updateItemsTable()
{
//... Query my DB
if (rdr.HasRows)
{
Panel_ItemList.Controls.Add(new LiteralControl("<!-- ItemList Panel -->\n"));
while (rdr.Read())
{
LinkButton lb = new LinkButton();
lb.Text = "Item";
lb.ID = "lbItem_" + strDBitemID;
lb.CommandName = strDBitemName;
lb.CommandArgument = strDBitemID;
lb.Command += new CommandEventHandler(LinkButton_Click);
Panel_ItemList.Controls.Add(lb);
}
Panel_ItemList.Controls.Add(new LiteralControl("<!-- END ItemList Panel -->\n"));
}
//...
conn.Close();
}
So the page loads fine and the timer reloads the table fine, but the LinkButtons do not fire the CommandEventHandler. This works fine if I remove the Timer.
Things I've tried:
I tried using Buttons rather than LinkButtons but this didn't help.
I read dozens of posts saying to add an ID to the LinkButton controls, but this didn't help either.
I believe the problem is when your adding the controls. For this to work the server controls need to be added in the Init event, or overriding OnInit(EventArgs).
Instead of explicitly creating the controls you could replace the panel with a repeater. Then bind your results from the database to the reader.
<asp:Repeater ID="TheRepeater" ...>
<ItemTemplate>
<asp:LinkButton onClick="LinkButton_Click" ...bind values to properties here />
</ItemTemplate>
</asp:Repeater>
code behind
TheRepeater.Visible = rdr.HasRows;
TheRepeater.DataSource = rdr;
TheRepeater.DataBind();
That being said, if all you want to do is alter the UI, that could easily be accomplished with jquery.
I believe the problem is in the page life cycle, since you are creating a dynamic control and adding the event after the page_init or page_load, its not getting hooked up correctly to the control, you could try out the following and see if it works:
Add page init:
protected void Page_Init(object sender, EventArgs e)
{
updateItemsTable();
}
and change the timer tick event to:
protected void Timer_Tick(object sender, EventArgs e)
{
itemPanel.Update();
}
and that should do the trick.
Hope this is of help.
Cheers.
You need to add postback trigger as following:
<asp:PostBackTrigger ControlID="SearchBrn"/>
Related
I wrote the following code for multiview where I am using gridview and datalist:
<ContentPlaceHolderID="ContentPlaceHolder1">
<div class="alert alert-success" >
<div class="divbtn" style="text-align:right">
<asp:LinkButton ID="gridbtn" runat="server" CssClass="glyphicon glyphicon-th" OnClick="gridbtn_Click"></asp:LinkButton>
<asp:LinkButton ID="listbtn" runat="server" CssClass="glyphicon glyphicon-th-list" OnClick="listbtn_Click"></asp:LinkButton>
</div>
</div>
<asp:MultiView runat="server" ID="multiview" ActiveViewIndex="0">
<asp:View runat="server" ID="gridview">
<uc1:GridControl runat="server" ID="GridControl"/>
</asp:View>
<asp:View runat="server" ID="listview">
<uc1:Listing runat="server" ID="Listing" />
</asp:View>
</asp:MultiView>
</asp:Content>
I am using two link buttons to call their respective views by firing two separate events as follows.
protected void listbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 1;
}
protected void gridbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 0;
}
Suppose, my datalist (Index=1) is active on my page and if there is a post back it should still be showing the datalist but on postback it automatically switches back to grid view (Index=0). I really need help with this!
You can save the index to a session variable and then read it back on post back like this:
To save:
Session["index"] = index.ToString();
Read it on page load like this:
Index = Session["index"];
You will need the session variable to maintain state per user session. If you want to maintain state for the application then you have to use the application variable.
Hi add the following code in your page_load event.
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
i think you are setting the multiview's active index to zero on every post back like follows
protected void Page_Load(object sender, EventArgs e)
{
multiview.ActiveViewIndex=0;
}
this will cause the multiview to set active index as 0 on every post back.To avoid this you have to set it as follows
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
}
New to webforms/c# I get a reference to the object in the onclick handler.
The reference to the object exists in the Repeater_ItemDataBound method.
After the curObj.CssClass = "XXXXX" has run the class object curObj is updated.
The page renders without the CSS class applied to the object.
I assume this is due to the LinkButton CSS does not apply to the Anchor tag that gets rendered in the end.
So how do I apply the CSS class to the actual rendered Anchor from the code behind?
// my aspx
<asp:Repeater ID="Repeater1" runat="server" onItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="my_btn" runat="server" OnCommand="cmdSelect_click" CommandArgument='<%# Eval("value") %>'><%# Eval("value") %></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
// my code behind
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (((MyObject)e.Item.DataItem).value == CurrentValue )
{
curObj.CssClass = "someCssClassHere";
}
}
protected LinkButton curObj;
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
curObj = (LinkButton)sender;
CurrentValue = int.Parse(e.CommandArgument.ToString())-1;
}
I dont understand when/where you want to set cssclass..
If you want to set it in ItemDataBound:
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton my_btn = (LinkButton)e.Item.FindControl("my_btn");
if (my_btn != null) my_btn.CssClass = "someCssClassHere";
}
or if you want to set after Click:
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
LinkButton my_btn = (LinkButton)sender;
my_btn.CssClass = "someCssClassHere";
}
That's not quite how templated controls such as the Repeater work.
Firstly, you should do a FindControl inside ItemDataBound to find your LinkButton, and apply the CSS to the item that is found.
Secondly, you don't wire up events for controls inside a Repeater that way; instead you handle the ItemCommand event of the Repeater.
Can you post the code you're using to bind the repeater ? It would be useful to know what your DataSource is, then I can post something that works.
This post might help as well - Linkbutton inside Repeater for paging ASP.Net
I created my gridview with checkboxes inside of it with this code.
<asp:GridView ID="GridView1" runat="server" Width="366px" autogeneratecolumn="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="SelectAllCheckBox" runat="server" AutoPostBack="true" oncheckedchanged="SelectAllCheckBox_OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="EachCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried check/uncheck it.
enter link description here
protected void SelectAllCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
String test = "test";
test = "newtest";
GridView1.DataSource = null;
GridView1.DataBind();
}
But it doesn't trigger any event.
enter link description here
I'm trying to find where my code is missing and searched so far but still can't.
Thank you for your help!
You must use OnItemCreated or OnItemDataBound and link your checkbox with your delegate
void Item_Created(Object sender, DataGridItemEventArgs e)
{
CheckBox cbx = (CheckBox)e.Item.FindControl("SelectAllCheckBox");
cbx.CheckedChanged += SelectAllCheckBox_OnCheckedChanged;
}
The code looks fine and works for me.
I suspect you might be binding the GridView on every postback.
When you click the CheckBox with the event attached it causes the page to refresh. If you bind the CheckBox on Page_Load (or any method that occurs on every trip to the server) it will bind the grid every time you click the CheckBox. In this case it will never get as far as firing your event.
If so, try checking for a postback before binding your GridView.
For example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gridview1.DataSource = myDataSource;
GridView1.DataBind();
}
}
I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}
I have Created A Custom Control which is a DropDownList with specified Items. I designed AutoPostback and SelectedCategoryId as Properties and SelectedIndexChanged as Event for My Custom Control.
Here Is My ASCX file Behind Code:
private int _selectedCategoryId;
private bool _autoPostback = false;
public event EventHandler SelectedIndexChanged;
public void BindData()
{
//Some Code...
}
protected void Page_Load(object sender, EventArgs e)
{
BindData();
DropDownList1.AutoPostBack = this._autoPostback;
}
public int SelectedCategoryId
{
get
{
return int.Parse(this.DropDownList1.SelectedItem.Value);
}
set
{
this._selectedCategoryId = value;
}
}
public string AutoPostback
{
get
{
return this.DropDownList1.AutoPostBack.ToString();
}
set
{
this._autoPostback = Convert.ToBoolean(value);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (SelectedIndexChanged != null)
SelectedIndexChanged(this, EventArgs.Empty);
}
I Want Used Update Panel to Update Textbox Fields According to dorp down list selected index.
this is my code in ASPX page:
<asp:Panel ID="PanelCategory" runat="server">
<p>
Select Product Category:
<myCtrl:CategoryDDL ID="CategoryDDL1" AutoPostback="true" OnSelectedIndexChanged="CategoryIndexChanged"
SelectedCategoryId="0" runat="server" />
</p>
<hr />
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanelEdit" runat="server">
<ContentTemplate>
<%--Some TextBoxes and Other Controls--%>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="CategoryDDL1" />
</Triggers>
</asp:UpdatePanel>
But Always The Selected Index of CategoryDDL1 is 0(Like default). this means Only Zero Value will pass to the event to update textboxes Data. what is the wrong with my code? why the selected Index not Changing? Help?
If your BindData() method is completely self-contained, move that from Page_Load to:
protected override void OnInit(EventArgs e)
{
BindData();
}
This will keep your dropdown list in your control from being rebound on every page load, which I assume is the problem from the code that you've posted.
If, however, your BindData() method requires information from the parent page, change the page load to:
protected void Page_Load(object sender, EventArgs e)
{
if(!this.Page.IsPostback) {
BindData();
}
DropDownList1.AutoPostBack = this._autoPostback;
}
This will allow your dropdown to be bound only on the first page load, and subsequent loads should be able to access the properties correctly.
Also, be sure to check your ASPX page to make sure you're not binding the ASCX control on every page load. This can be resolved in the same way on the parent page.