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
}
Related
I have the below Image Button which resides in a Repeater:
<asp:ImageButton PostBackUrl='<%# DataBinder.Eval(Container.DataItem, "VesselId", "getattachment.do?VesselId={0}") %>' CausesValidation="false" ID="insurancestatus" runat="server"/>
So every time the user clicked on the button they were shown their attachment.
The requirement has since changed, and the attachment must open on a new page so I added the following to the Repeaters ItemDataBound event:
ImageButton imagebuttonInsurance = (ImageButton)e.Item.FindControl("insuranceStatus");
imagebuttonInsurance.OnClientClick = String.Format("aspnetForm.target = '_blank'; return true()");
Now when I click on the image it does open another page but it just reloads the previous page on the new page. I tried removing the PostBackUrl and wiring up the following click event:
protected void insurancestatus_Click(object sender, ImageClickEventArgs e)
{
Vessel vessel = (Vessel)e.Item.DataItem;
Response.Redirect(String.Format("~/Secure/getattachment.do?VesselId={0}", vessel.VesselId));
}
But I couldn't get a handle on the item so cannot use the VesselId for the link, where am I going wrong here?
Use the second method, but use the command argument property like:
<asp:imagebutton runat=server...... commandargument='<%# Eval("vesselid") %>' />
then:
protected void insurancestatus_Click(object sender, ImageClickEventArgs e)
{
string vesselId = ((ImageButton)sender).CommandArgument;
Response.Redirect(String.Format("~/Secure/getattachment.do?VesselId={0}", vesselId));
}
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'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"/>
I have a ListView setup with LinqDataSource and a button that triggers search function. To avoid display data on page_load, I set ListView's DataSourceID in the Click event of the search button, bind it and set result data in LinqDataSource's Selecting event. It works as I expected but It does't look pretty to set DataSourceId in the button Click event every time the search button is clicked. How can I do this in a better and clearer way?
ASPX code:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="WebApplication1.DataClasses1DataContext" EntityTypeName=""
TableName="Persons" onselecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>
<asp:ListView ID="ListView1" runat="server" >...</asp:ListView>
<asp:Button ID="Search" Text="Search" runat="server" Click="Search_Clicked"/>
ASPX.CS code:
protected void Search_Clicked(object sender, EventArgs e)
{
ListView1.DataSourceID = LinqDataSource1.ID;
ListView1.DataBind();
}
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
//Search Criteria from CheckBoxList and TextBox applied here.
DataClasses1DataContext data = new DataClasses1DataContext();
var query = from result in data.Persons
where result.ID > 2
select result;
e.Result = query;
}
I honestly don't see anything wrong with your approach, however, if you don't like it, an alternate approach would be to just statically set the DataSourceID in your ListView markup as usual, but set Visible="False", and only make it visible once the button has been clicked.
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");