I have some asp.net pages.
I used the masterpage to implement common control.
There is a listview on the masterpage.
And there is a label on the contentpage.
I want to implement below.
When user select a node on the listview of masterpage,
the label text on the contentpage is changed into the text of the node.
How can i implement this.
Could you give me some advice or some link about my question?
Thank you in advance.
On Master1.master, I used ItemCommand event on ListView
<form id="form1" runat="server">
<asp:ListView ID="List1" runat="server" onitemcommand="List1_ItemCommand">
<ItemTemplate>
<p>
<asp:label ID="ItemLabel" runat="server" text="<%#Container.DataItemIndex %>" />
<asp:LinkButton ID="ItemLink" runat="server" CommandName="SelectItem" Text="Select" />
</p>
</ItemTemplate>
</asp:ListView>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
And Master1.master.cs, store selected item text to public property
public partial class Master1 : System.Web.UI.MasterPage
{
public string selectedText { get; set; }
protected void List1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "SelectItem")
{
selectedText = ((Label)e.Item.FindControl("ItemLabel")).Text;
}
}
}
Then in Content1.aspx , add a label with id Label1
<asp:Label ID="Label1" runat="server" />
Finally in Conetnt1.aspx.cs, read the property "selectedText" on prerender event (which comes after select click)
protected void Page_PreRender(object sender, EventArgs e)
{
var myMaster = (Master1)this.Master;
Label1.Text = myMaster.selectedText;
}
Related
<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
Code Behind:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
how i get the reference to the "lblMessage" in Delete_Command.
Try this:
protected void Delete_Command(object sender, CommandEventArgs e)
{
LinkButton button = (LinkButton)sender;
Label label = (Label)button.NamingContainer.FindControl("lblMessage");
// do something with the label
}
If you:
Have bound the repeater
Have ViewState enabled
Do not re-bind the repeater earlier in the post back
this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.
Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.
means are you want find a lable in Delete_Command event?
in aspx
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
<asp:Label ID="lblMessage" run="server">
</ItemTemplate>
</asp:Repeater>
in aspx.cs
protected void Delete_Command(object sender, CommandEventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
Label lblMessage = item.FindControl("lblMessage") as Label;
if (lblMessage != null)
{
lblMessage.Text = "";
}
}
}
If You want to make it in your way use following code in
protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
{
(((LinkButton)source).NamingContainer).FindControl("lblName")
}
Another approach.. But something that you can buy
aspx
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
<asp:LinkButton runat="server" CommandName="Delete_Command" Text="sd"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
protected void Delete_Command(object sender, CommandEventArgument e)
{
if(e.CommandName != null)// Conditional Check
{
Label label = e.Item.FindControl("lblMessage");
// do something with the label
}
}
I'm trying to create a simple web page with a navigation bar and some usercontrols (ascx) programmatically loaded.
All controls are inside an update panel.
When I click on a link button (from the navigation bar) I do the following things:
I save the current usercontrol using viewstate.
Than I reload the current usercontrol.
My 'page_load' always reloads the current control.
Always assigning the same ID to the programmatically loaded control allows me to save the usercontrol viewstate.
So everything look good except one little thing: the usercontrol viewstate in not available during the usercontrol Page_Load!
Look below for (* HERE).
The 'txtTest.Text' value is always "0" (also during postback).
It seems that the user control viewstate is restored after the (usercontrol) Page_Load.
How is it possible?
--- "DEFAULT.ASPX": ---
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sm" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="pnlMain" runat="server">
<ContentTemplate>
<div class="links">
<asp:LinkButton ID="lnkButton1" runat="server" OnClick="lnkButton1_Click" Text="Link 1"></asp:LinkButton>
<asp:LinkButton ID="lnkButton2" runat="server" OnClick="lnkButton2_Click" Text="Link 2"></asp:LinkButton>
</div>
<br />
<asp:Panel ID="pnlCtrl" runat="server"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
--- "DEFAULT.ASPX.CS": ---
private string CtrlAscx
{
get
{
if (ViewState["CtrlAscx"] == null)
{
ViewState["CtrlAscx"] = String.Empty;
}
return ViewState["CtrlAscx"].ToString();
}
set
{
ViewState["CtrlAscx"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
loadMyControl();
}
private void loadMyControl()
{
if (!String.IsNullOrEmpty(CtrlAscx))
{
pnlCtrl.Controls.Clear();
Control c = LoadControl(CtrlAscx);
c.ID = CtrlAscx + "ID"; // this line is mandatory in order to mantain the usercontrol viewstate
pnlCtrl.Controls.Add(c);
}
}
protected void lnkButton1_Click(Object sender, EventArgs e)
{
CtrlAscx = "Control1.ascx";
loadMyControl();
}
protected void lnkButton2_Click(Object sender, EventArgs e)
{
CtrlAscx = "Control2.ascx";
loadMyControl();
}
-- "CONTROL1.ASCX" --
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Control1.ascx.cs" Inherits="WebTest.Control1" %>
Control1: <asp:TextBox id="txtTest" runat="server" Text="0"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" />
-- "CONTROL1.ASCX.CS" --
public partial class Control1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (txtTest.Text == "0") // * HERE
{
txtTest.Text = "1";
}
}
}
Try the EnableViewState="true" attribure on txtTest as well as on your custom user control when creating it :
.
.
c.EnableViewState = true;
pnlCtrl.Controls.Add(c);
I have a repeater which includes a radio button in each item, and the whole thing sites inside an update panel. When I select a radio button the whole page reloads. Why is it not just updating the update panel. I've reduced this to a pretty simple example to avoid clutter. Code here...
ASPX...
<asp:ScriptManager ID="SM1" runat="server" />
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater runat="server" ID="history">
<ItemTemplate>
<asp:RadioButton runat="server" ID="radioButton" AutoPostBack="true" GroupName="HistoryGroup" OnCheckedChanged="RadioButton_CheckChanged" /><br />
</ItemTemplate>
</asp:Repeater>
<p><asp:Literal runat="server" ID="output" /></p>
</ContentTemplate>
</asp:UpdatePanel>
Code...
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> list = new List<int>();
for (int i = 0; i < 20; i++)
list.Add(i);
history.DataSource = list.ToArray();
history.DataBind();
}
}
protected void RadioButton_CheckChanged(Object sender, EventArgs e)
{
output.Text = DateTime.Now.ToString();
}
}
Setting ClientIDMode=Auto on the RadioButton should fix it (it's an infamous .NET bug, http://connect.microsoft.com/VisualStudio/feedback/details/584991/clientidmode-static-in-updatepanel-fails-to-do-async-postback)
please add up1.Update() after output.Text = DateTime.Now.ToString(). Your RadioButton is not the trigger for updatepanel
Turns out the solution was to remove the GroupName from the RadioButton. When I remove this tag it fires asynchronously and just updates the panel. I don't actually need this tag anyway (due to the known bug where GroupName doesn't work on RadioButtons in Repeaters) as I handle the grouping within my click event (i.e. uncheck any other RadioButtons of the same name in other repeater items).
as you can see in my code... i have a label inside ItemTemplate and what i want is when i click on that particular control i would like to access to the label so that i can update the status...
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" OnItemCreated="Repeater1_ItemCreated" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
Book:
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<Mycontrol:Content1 ID="EmpControl" runat="server" OnMyControlClick="EmpControl_clicking" />
<br />
</ItemTemplate>
</asp:Repeater>
protected void EmpControl_clicking(object sender, EmployeeEventArgs e)
{
// how do i get access to the lblStatus???
}
You will need to use the FindControl method to access controls within templates:
protected void EmpControl_clicking(object sender, EmployeeEventArgs e)
{
MyControl myControl = (MyControl)sender;
Label c = (Label)myControl.Parent.FindControl("lblStatus");
}
Can someone tell me how I can get this to work. I want to distinguish dropdown controls inside a repeater control. I understand now about the lifecyle and how the buffer is already writen, but what are my alternatives? Here is what happens
Code File
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
Aspfile
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="AdTitle<%# Container.ItemIndex %>" runat="server">
<asp:ListItem Selected="True" Value="" Text=""/>
<asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
<asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
<asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
<asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
<asp:ListItem Selected="False" Value="Other" Text="Other"/>
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
Returns this error
Parser Error Message: 'AdTitle<%# Container.ItemIndex %>' is not a valid identifier.
I want to distinguish dropdown
controls inside a repeater control.
You don't need to. Here's some sample code that might help you out:
Markup:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>
<asp:Label runat="server" ID="lblMsg" Text="Click a button" />
<hr />
<asp:Repeater runat="server" ID="rptAwesome" onitemcommand="rptAwesome_ItemCommand"
>
<ItemTemplate>
<asp:Button runat="server" ID="btnAwesome"
Text='<%# "Button #" + Container.ItemIndex %>'
CommandArgument='<%# Container.DataItem %>'/><br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Codebehind:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var x = new int[] { 1, 2, 3, 4, 5 };
rptAwesome.DataSource = x;
rptAwesome.DataBind();
}
}
protected void rptAwesome_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Button btnAwesome = (Button)e.CommandSource;
lblMsg.Text = string.Format("btnAwesome.ID = {0}, e.CommandArgument = {1}", btnAwesome.ID, e.CommandArgument);
}
}
You cannot create a Unique ID inside a repeater through the markup like that. You can however retrieve the dropdown in the codebehind by using FindControl on the Repeater.
Dim adTitle As DropDownList = MyRepeater.FindControl("AdTitle")
If (Not adTitle Is Nothing) Then
''Do something here
End If
Ok... in a nutshell you can't do what you're trying to do in the manner you are currently doing it.
The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />
I can see what your trying to do, but I don't really understand Why...
A repeater control will contain 1 item, per item in your datasource, so it's perfectly fine to just call your DropDownList ID="AdTitle" as that will be a different 'AdTitle' from the one in the next row.
To get them back server side, you would just iterate through the Items in your Repeater & FindControl("AdTitle") to get the specific DropDownList for that item.
If it's absolutely necessary to have the IDs incrementing, you'll need to do this programmatically (probably on the ItemDataBound event to create a DropDownList and add it to your ItemTemplate.
Expanding on BC's answer: I recently had the same issue, albeit with a checkbox, I needed to set the ID as I used that when persisting choices. A bit clunky but I did it when on Item Data Bound.
Here's the markup:
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>Header<br /><br /></HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
...other controls removed for brevity
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Submit" Text="Submit" />
</FooterTemplate>
</asp:Repeater>
I set up and bound a collection of this class
public class mst
{
public int Id { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public bool NumberRequired { get; set; }
}
Do the necessary in Page_Load
Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);
Repeater1.DataSource = listOfMsts;
Repeater1.DataBind();
Followed by, this where I set ID:
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var mst = (mst)e.Item.DataItem;
((CheckBox)e.Item.FindControl("cb")).ID = mst.Id.ToString();
}
}
So when the submit button is hit I can loop thru and grab the Id of those checked and save:
protected void Submit(object sender, EventArgs e)
{
foreach (var item in Repeater1.Items)
{
var checkboxes = ((RepeaterItem)item).Controls.OfType<CheckBox>().ToList();
if (checkboxes[0].Checked)
{
Save(checkboxes[0].ID);
....other stuff
}
}
}
You could add an event handler to Repeater.ItemDataBound and create the control within the handler using the reference to the item container.