Link button control using asp.net - c#

I have set of link buttons in my masterpage and one button in my content page.
i want when i click the any one of link button in the masterpage the value in the content page will change. How i can do this. Can any one able to help me because i am new in asp.net
Thank you

you can do something like this from your master page
var linkButton = ContentPlaceHolder1.FindControl("contentPageButton1") as LinkButton;
linkButton.Text = "Foo";

You can set it up with events.
In your master page:
public event EventHandler<EventArgs> SomethingChanged;
In your content page:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
((MyMasterPage)Page.Master).SomethingChanged += (s, ev) => UpdateStuff();
}

Related

Command event not firing asp.net for dynamically created control

I've got the following code that creates an image button dynamically on a li html tag. When I click the image button it does not fire the event. What am I doing wrong please?
Code that generates the control:
ImageButton cmdEdit = new ImageButton();
cmdEdit.ImageUrl = "~/Images/phone_book_edit.png";
cmdEdit.ID = "cmdEdit" + recordcount.ToString();
cmdEdit.Attributes["class"] = "liQuestionsLabel2";
cmdEdit.Width = 30;
cmdEdit.CommandName = "Edit";
cmdEdit.CommandArgument = (recordcount - 1).ToString();
cmdEdit.Command += new CommandEventHandler(EditQuestion_Command);
li.Controls.Add(cmdEdit);
Event code:
protected void EditQuestion_Command(object sender, CommandEventArgs e)
{
Response.Write("here");
}
Dynamically generated controls lose their state after they are rendered. For you to access them again once you postback, you will have to recreate them in the code-behind along with recreating the attached events as well.
Put the above code in your Page_Load but not inside the if(!IsPostback) so that it gets recreated and you can then access its event.
Other than that, the above code works fine for me when I recreated the same control in my page.

Postback from Dynamic Content

I am dynamically building some HTML content on a WebForms page, using HtmlGenericControl().
HtmlGenericControl p = new HtmlGenericControl("p");
// ...
ListItems.Controls.Add(p);
But now I need to add a button to the paragraph created above, and the button needs to do a postback when clicked.
Must I completely rework this code to load child controls, which can contain real server-side buttons? Or is there a way to inject buttons capable of some type of postback?
Here's a test method and test EventHandler that, if called, will add a button control with a server-side Click event to ListItems. The commented-out code will change the markup of the ListItems object if InnerHtml is available but I rethought that approach and assumed the existence of a textbox on the page that would show the results of the click event firing. There are a number of concerns that you'll want to handle if you need those child controls to be present from one postback to another, though- for instance, if Test() isn't called every time the page loads that button won't be created and there will be no way to call its EventHandler unless other controls also call it.
public void Test()
{
System.Web.UI.HtmlControls.HtmlGenericControl p = new System.Web.UI.HtmlControls.HtmlGenericControl("p");
p.InnerHtml = #"<strong>Test</strong>";
// ...
ListItems.Controls.Add(p);
Button b = new Button();
b.ID = "cmdTest";
b.Text = "Test";
b.Click += new EventHandler(test_Click);
p.Controls.Add(b);
}
protected void test_Click(object sender, EventArgs e)
{
// ListItems.InnerHtml = "Test button clicked";
txtTestResults.Text = "Test button clicked at " + DateTime.Now.ToShortTimeString();
}

asp.net saving control values on postback

I have a dynamic table with some textboxes (also dynamic) and some buttons which do postback onclick.
How can I make the page remember what text was entered in the boxes after postback, after clicking a button?
You have to create controls in a tymer click event.For that Create a new user control. Add public Properties in it for adding how much controls u have to add. And in Web user control Page INit and Page_load event Add the required number of controls. Hope this will work.
//IN web user control aspx page add a place holder in which u add your dynamic controls
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:PlaceHolder runat="server" ID="mycontrol"/>
// WEb User Control Code Behind
// Create public properties
public int totalnoOfcontrols
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
// save values here
}
}
protected void Page_Init(object sender, EventArgs e)
{
// create dynamic controls here
TextBox t = new TextBox();
t.Text = "";
t.ID = "myTxt";
mycontrol.Controls.Add(t);
}
You have to use Page_Init/Load event handler to create controls runtime (dynamically).
For that you can use the ViewState
string data = ViewState["myData"];
ViewState["myData"] = data;

tab container - event is not firing

i am creating a tab container at runtime and make 1 of the column member as the tab header
once user click on the tab i want to fire the event ActiveTabChanged.. if autopostback=true the entire tab container will gone but it got commend inside the event. if autopostback=false it can't go in the event and nothing happen at the layout.. hence i change the concept of my code..what i want is when user click on tab.. event are fire > everything remain same > next asp.net function will be call from the event. below are my coding
Remark- TabC is the tab container
<script type="text/javascript">
function ActiveTabChanged(sender, e) {
var Current_Tab = $get('<%#TabC.ClientID%>');
Current_Tab.innerHTML = sender.get_activeTab().get_headerText();
__doPostBack('TabC', sender.get_activeTab().get_headerText());
//Highlight(TabC);
}
</script>
in the body
<asp:TabContainer ID="TabC"
runat="server"
OnClientActiveTabChanged="ActiveTabChanged"
OnActiveTabChanged="ActiveTabChangedServer"
ActiveTabIndex="0"
/>
at Page Load
UpdatePanel oUpdatePanel = new UpdatePanel();
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
protected void Page_Load(object sender, EventArgs e)
{
trigger.ControlID = "TabC";
trigger.EventName = "ActiveTabChanged";
oUpdatePanel.Triggers.Add(trigger);
Page.Header.DataBind();
ScriptManager.RegisterAsyncPostBackControl(TabC);
if (!Page.IsPostBack)
{
//runtime generate tab and gridview
Bind_Category_with_Tab();
}
}
error i get
output
the main problem is.. when i fire any event of the component in the page every things will disappear.. if the event can be trigger (AutoPostBack="true")
what mistake i have been done here? please to give a help here.. thanks advance

ASP.NET Button click redirect to new page

I have created a Button programmatically.
I now want to redirect to a new aspx page on the click of this Button, such that we will enter into one of the methods of the new page (like page_load(), etc.)
Is this possible?
Eg:
Button oButton = new Button;
oButton.Text = "NextPage";
// Redirect to "Redirect.aspx" on click
But I am not able to find an entry point in Redirect.aspx where I can do some changes to the UI components once we get redirected to "Redirect.aspx"
Regards,
Parag
You need to handle Click event of oButton.
Button oButton = new Button();
oButton.Text = "NextPage";
oButton.Click += (sa, ea) =>
{
Response.Redirect("Redirect.aspx");
};
You can use query string parameters and depending on the value of param call the appropriate method in Page_Load of Redirect.aspx
e.g.
Redirect.aspx?val=1
in Redirect.aspx
protected void Page_Load(...){
string var = Request.QueryString["val"];
if(var == "1")
some_method();
else
some_other_method();
}
You could also use the PostBackUrl property of the Button - this will post to the page specified, and if you need to access items from the previous page you can use the PreviousPage property:
Button oButton = new Button;
oButton.Text = "NextPage";
oButton.PostBackUrl = "Redirect.aspx";
Then in you wanted to get the value from, say, a TextBox on the previous page with an id of txtMyInput, you could do this (very simple example to give you an idea):
void Page_Load(object sender, EventArgs e)
{
string myInputText = ((TextBox)PreviousPage.FindControl("txtMyInput")).Text;
// Do something with/based on the value.
}
Just another example of how to accomplish what I think you're asking.
See Button.PostBackUrl Property for more info.

Categories