codebehind is not setting CssClass on a link button in a repeater - c#

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

Related

How to get Text property of TextBox after ItemCommand event

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();
}
}

event attched to link button inside gridview is not working?

I have a grid in asp.net, inside the asp.net i am binding data as linkbutton when clicking on link button I need to call a method in code behind. the attached event is not woking in my code. how i can solve this?
my code is similar like this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
link.Click += new EventHandler(this.onLinkClick);
e.Row.Cells[0].Controls.Add(link);
}
}
protected void onLinkClick(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string value = btn.CommandArgument;
TextBox1.Text=value;
}
You have to call the function that binds the source to the GridView everytime in the Page Load
ex.
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridView();
}
Because there is not logic for adding or not the link button(I guess you have to add it for each record) why don't you add it at design time?
<asp:GridView ID="GridView1" runat="server">
....
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
......
</asp:GridView>
Make sure that AutoEventWireup="true" on the page
Handle RowCommand event of GridView to handle "events" of buttons and you are adding LinkButton dynamically then Data Binding must be performed either at Page_Init or Page_Load.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
e.Row.Cells[0].Controls.Add(link);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = e.CommandArgument.ToString();
TextBox1.Text=value;
}
You need to hook up the event handler for your dynamic button in the GridView's RowCreated event or it won't fire. Then use "FindControl" in the RowDataBound event handler. Personally, I don't like this model at all but sometimes it's unavoidable.
You should use the GridView's <asp:ButtonField> with the grid's RowCommand event. This way, you're not the one creating the dynamic control and wiring up the events.
Here's an article on how to use it.

LinkButton not working within UpdatePanel

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"/>

Add LinkButtons during OnDayRender event of ASP.NET Calendar Control

So I need to add some link buttons to each cell of a asp:calendar control dynamically based on data in the database. I wondering what the best way to do this is so that the the link buttons will be wired up to their events on postbacks (as to my knowledge creating the link buttons on the Day Render event and adding them there will occur after the LinkButton events would have fired).
Does anyone have a nice way to handle this?
EDIT: This is a hack but it works, you will have to get crafty with the event naming...etc
Markup:
<asp:Calendar id="calendar1"
OnDayRender="DayRender"
runat="server">
<asp:LinkButton ID="LinkButton1" style="display:none;" runat="server"
onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
Code-Behind:
protected void DayRender(Object source, DayRenderEventArgs e)
{
LinkButton lb = new LinkButton();
lb.ID = "LinkButton1";
//set all your props
lb.Attributes.Add("href",
"javascript:__doPostBack('" + Calendar1.UniqueID + "$" + lb.ClientID +"','')");
e.Cell.Controls.Add(lb);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//do something
}

Access linkbutton from datalist on pageload with c# asp.net

I have this linkbutton within a datalist and I'm trying to get access to the datalist on the pageload so i can set the linkbutton to be enabled or not based on the user's role.
<asp:DataList id="dlRecommendations" runat="server" DataKeyField="Key" Width="900">
<ItemTemplate>
<asp:LinkButton id="lnkEdit" Text="Edit" Runat="server" CommandName="Edit">
</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
Within the page load I want to be able to access the linkbutton to enable or disable it based on the user's role.
private void Page_Load(object sender, System.EventArgs e) {
//perhaps something like this:
lnkEdit.Enabled = false;
....
}
I think you will be populating the datalist the first time page is loaded. So just wireup ItemDataBound, find link and disable it.
void dlRecommendations_ItemDataBound(object sender, DataListItemEventArgs e)
{
var link = e.Item.FindControl("lnkEdit") as LinkButton;
if (link != null)
{
link.Enabled = UserHasRight;//if user has right then enabled else disabled
}
}
DataList is a databound control - it builds rows only when data is supplied.
To access link inside row use ItemDataBound event and access e.Item.FindControl("linkId");

Categories

Resources