I have a repeater that has a LinkButton in one of its columns and I have the onclick event wired up. When the user clicks on one of the options, I need to know in the event which LinkButton they clicked on. What is the best practice to do this?
You should use the OnCommand event instead of OnClick use some CommandName and CommandArgument to distinguish b/w items. This MSDN page has an example.
Normally CommandArgument='<%#Eval("Id") is used for such purpose
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%#Eval("Id") %>' CommandName="commandName"></asp:LinkButton>
and then it will be like...
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName == "commandName")
{
Int32 id = Convert.ToInt32(e.CommandArgument);
}
}
What you want to do is wire up the Repeater's ItemCommand event and not use the LinkButton's OnClick event. Instead wire up the CommandName of the LinkButton.
When the ItemCommand fires you'll be able to tell what button triggered it based on the CommandName that was set on the button. You'll also have access to all the controls in that row of the Repeater.
MSDN
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx
Check with this code
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
repTriggers.DataSource = new int[3] { 0, 1, 2 };
repTriggers.DataBind();
}
}
protected void repTriggers_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "trigger")
{
LinkButton btn = e.CommandSource as LinkButton;
if (btn != null)
{
lblUpdate.Text = "Update triggered by " + btn.ID + e.Item.ItemIndex.ToString();
}
// [Steve] removed UpdatePanel2.Update()
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="TheScriptManager" runat="server"></asp:ScriptManager>
<%-- [Steve] removed UpdatePanel1 --%>
<asp:Repeater ID="repTriggers" runat="server" OnItemCommand="repTriggers_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkTrigger" runat="server" Text="Trigger" CommandName="trigger"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="conditional">
<%-- [Steve] added repTriggers as an AsyncPostBackTrigger --%>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="repTriggers" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblUpdate" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Source URL http://forums.asp.net/p/1062060/1528071.aspx
Related
I have one site master page and one content page. Whenever I click button in content page it is not triggering click event method to execute.
SiteMaster Code:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body background="../../Content/city-balcony#2x.jpg">
<form id="form1" runat="server">
<div class="page">
<div id="header">
<div id="title">
<h1>Welcome to E-Meeting</h1>
</div>
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %>
</div>
<div id="menucontainer">
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<%
if (Request.IsAuthenticated)
{
%>
<li><%: Html.ActionLink("Chat", "ViewPage1")%></li>
<% }
%>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
</ul>
</div>
</div>
<div id="main">
<%="Last communicated at : " + DateTime.Now.ToLongTimeString()%>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
<br />
<br />
</div>
</form>
</body>
</html>
Content Page:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Modified by click event";
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ChT
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ChT</h2>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="On Load"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Whenever I click button I want label text to be "Modified by click event" but it remains same "On Load". Can someone please help
Try adding a trigger in your update panel as shown:
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
And Check ScriptManager's EnablePartialRendering and remove it if it presented or set it to true
you can add a method using C# to trigger the event.
double-click button to create a button click event.
add a code to change the label text to "Modified by click event"
for example,
protected void Button1_Click(object sender, EventArgs e){
Label1.text = "Modified by click event";
}
by the way, you could get rid of Text="On Load" in your aspx code. (your code would be: <asp:Label ID="Label1" runat="server"></asp:Label>, then add a page_load method in your .cs file:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Label1.text = "On load";
}
in this case, it will display "on load" when you open the webpage, and it will display "Modified by click event" when you clicked button.
hope it
I've got MasterPage and Content Page. On the ContentPage there is some label and i want to change it's text when clicked on button on the content page without refreshing the page.
Default.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>
<script runat="server">
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Label SensorTemperatureLabel = (Label)this.Master.FindControl("SensorTemperatureLabel");
if (SensorTemperatureLabel != null)
{
SensorTemperatureLabel.Text = "TEST";
}
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" />
</asp:Content>
MasterPage.master
Part of the code with label
<div class="sidebar">
<!-- insert your sidebar items here -->
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="SensorTemperatureLabel" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
I have prepared you complete solution to show you how you can do this.
First sample master. Take notice I have added ScriptManager to page and target label is inside content template:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebTester.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="SensorTemperatureLabel" runat="server" Text="TEMP"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Now here is content page, button is also placed inside content template:
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebTester.WebForm1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Test" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
And finally here is the code, I get refrence to label by using FindControl:
protected void Button1_Click(object sender, EventArgs e)
{
Site1 site = this.Master as Site1;
if (site != null)
{
Label SensorTemperatureLabel = site.FindControl("SensorTemperatureLabel") as Label;
SensorTemperatureLabel.Text = DateTime.Now.ToString();
}
}
This code will update label on master page without complete page postback.
If the label is not in <ContentTemplate />, you can access with javascript
var SensorTemperatureLabel = document.getElementById("<%=SensorTemperatureLabel%>");
SensorTemperatureLabel.innerHTML = "newValue";
Otherwise, the ID is generated and you'll need
var SensorTemperatureLabel = document.getElementById("<%= this.Master.FindControl("SensorTemperatureLabel").ClientID %>");
I hope my answer help in this case
if you have Label in master page named " Label1" then
you can change value there like this
VB
Dim lb as label= me.master.findcontrol("Label1"):lb.text="Hello worold"
Peace upon you all.
I have listview and every row has a Drop Down List and a hyperlink control.
What i'm trying to do is change the navigate url for the hyperlink based on the selection of the drop down list. How would I get the row that the control posted back from so I can find the hyperlink control?
You may try the following:
<%# Page Language="C#" %>
<script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyList.DataSource = Enumerable.Range(1, 5);
MyList.DataBind();
}
}
protected void DDLChange(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
var link = (HyperLink)ddl.Parent.FindControl("MyLink");
link.NavigateUrl = ddl.SelectedValue;
link.Text = ddl.SelectedValue;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:ListView ID="MyList" runat="server">
<ItemTemplate>
<div>
<asp:DropDownList runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDLChange">
<asp:ListItem Value="http://www.google.com" Text="http://www.google.com" />
<asp:ListItem Value="http://www.bing.com" Text="http://www.bing.com" />
<asp:ListItem Value="http://www.yahoo.com" Text="http://www.yahoo.com" />
</asp:DropDownList>
<asp:HyperLink runat="server" ID="MyLink" NavigateUrl="http://www.google.com" Text="http://www.google.com" />
</div>
</ItemTemplate>
</asp:ListView>
</form>
</body>
</html>
How to prevent the content of the UpdatePanel from the PostBack which is occurred in the whole page ?
I have the following code:
<head runat="server">
<title></title>
<script type="text/C#" runat="server">
// I don't want it to call this event handler
// untill I update the UpdatePanel manually by UpdatePanel1.Update();
protected void TextBox2_Load(object sender, EventArgs e)
{
((TextBox)sender).Text = DateTime.Now.ToString();
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" OnLoad="TextBox2_Load">
</asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Unnamed1_Click" />
<asp:TextBox ID="TextBox1" runat="server" />
</div>
</form>
</body>
</html>
The previous code will write in the TextBox2 in spite of it exists inside an UpdatePanel with Conditional for UpdateMode.
I want it to update only when I call UpdatePanel1.Update();
Any help!
If you put Button1 and TextBox1 in the same UpdatePanel as TextBox2, this should give you your desired functionality.
If you want to keep them in separate UpdatePanels, you can, but you'll need to specify a trigger on UpdatePanel1 for the Unnamed1_Click event (or call UpdatePanel1.Update() in code).
Try using IsInAsyncPostBack:
protected void TextBox2_Load(object sender, EventArgs e)
{
if (ScriptManager1.IsInAsyncPostBack == true)
((TextBox)sender).Text = DateTime.Now.ToString();
I want to bind dropdownlist to List<MyIem>,
in code behind.
<asp:DropDownList ID="listCategories" runat="server" Height="20px" CssClass="CategoryDropList" SelectedValue='<%# Bind("ParentId") %>' AutoPostBack="false" Width="300px">
Without using ObjectDataSource !
How can I Bind it to the dropdown list? In what event?
Also SelectedValue='<%# Bind("ParentId") %>' should work! (I mean the dropdownlist binding should occur before this!)
Made an example which will set the dropdown in the DataBound event.
Here is the markup
The way to use the ddl, is to find it with findcontrol() during DataBound event.
When you have the control in the DataBound event, you can also bind the dropdown to your List<>
Hope this helps.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:FormView ID="FormView1" runat="server" ondatabound="FormView1_DataBound">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
Here is the code behind:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Some");
list.Add("Other");
FormView1.DataSource = list; //just to get the formview going
FormView1.DataBind();
}
protected void FormView1_DataBound(object sender, EventArgs e)
{
DropDownList ddl = null;
if(FormView1.Row != null)
ddl = (DropDownList) FormView1.Row.FindControl("DropDownList1");
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Two"));
}
}
}
You can populate the DropDownList with another DataSource, assuming the valid values are in the database. Check out this video:
http://msdn.microsoft.com/en-us/data/cc546554.aspx
It's using an EntityDataSource instead of an ObjectDataSource, but the principle should still work.
If you want a "(none)" type option for null, see section "Converting Null in Template Fields" on this page:
http://msdn.microsoft.com/en-us/library/ms366709.aspx
Specifically:
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
DataTextField="Name" DataValueField="EmployeeID"
SelectedValue='<%# Bind("ReportsTo") %>' AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="">(none)</asp:ListItem>
</asp:DropDownList>
Notice the the "AppendDataBoundItems" attribute and the "asp:ListItem" element.
well i was facing a similar problem. i noticed that you were trying to add it to and not which can be a major cause that you did not see it.
i have however worked on both the solutions provided above and found this worked for me :-
<EditItemTemplate>
<asp:DropDownList ID="ddlStream" runat="server" SelectedValue='<%# Bind("Stream") %>'>
<asp:ListItem>Common</asp:ListItem>
<asp:ListItem>Mechanical</asp:ListItem>
<asp:ListItem>Electronics</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" id="something" text='<%# Eval("Stream")%>'/>
</ItemTemplate>
hope this helpes you.