I have this code in my codebehind:
for (int i = 0; i < linkList.Count; i++)
{
var link = UppercaseFirst(linkList[i]);
var linkButton = new LinkButton
{
Text = link + " > ",
ID = Convert.ToString(i),
CommandArgument = urlList[i]
};
linkButton.Command += new CommandEventHandler(lnkWeb_Click);
bcHolder.Controls.Add(linkButton);
}
and here is the lnkWeb_Click method:
protected void lnkWeb_Click(object sender, CommandEventArgs e)
{
var url = e.CommandArgument.ToString();
//code...
}
This method is not getting triggered when I click on one of those generated linkbuttons.
Anyone have any idea what the problem is?
Tried OnCommand="lnkWeb_Click" in the aspx file and the method got trigged, but not those that I generate by code. They dont even have OnCommand="lnkWeb_Click" attribute.
The problem here is with the control life cycle. If you want to handle events of some control properly - you have to add this control to the page on every page loading process, that is on every postback.
Look what happens in your case:
Initial button is clicked
During the post back your dynamic link buttons are added to the page, event handlers are assigned to them
User clicks on the newly generated link button
During post back these dynamic link buttons are not added to the page again, ASP.NET does not know the origin of a event so it does not call the handler.
To fix this you might need to store in the View State information about link buttons that have to be added (please do not store the controls themselves, that would be a huge overhead). Also pay attention to their IDs - they have to be the same for the same controls.
Update. Some more hints on the View State solution.
Basically you need some indicator that during the page loading you need to create some dynamic link buttons. The very basic way to do it is to store the list of the link button identifiers (or texts, or both) and then during Page_Load check if there is anything stored in View State. For example:
// Property to access the view state data
protected List<string> Links
{
get { return ViewState['links']; }
set { ViewState['links'] = value; }
}
...
protected void Page_Load(object sender, EventArgs e)
{
...
if (this.Links != null && this.Links.Count > 0)
{
// inside this method you create your link buttons and add them to the page
// you actually have this code already
RenderLinkButtons();
}
}
...
// Not sure about what name you have here
protected void InitialButtonHandlerName(object sender, EventArgs e)
{
List<string> linkList = ...; //your variable, guessing a type
// this is exactly the method you use already to add links to the page
// just one more action added to it - store info about these links into View State to use it on later post backs
this.Links = linkList;
RenderLinkButtons();
}
Please use it just a point in right direction - you might have different implementation depending on your requirements and preferences. But I hope concept is clear now.
Related
i like to get initial value to my DDL and write ddl1.Text="6" - it works fine..
i try to do the same to a DDL which is part of a simple usercontrol(3 DDLs which create a date) - this doesn't work!!!
in default.aspx i tried-
DateUserControl2.SetD("17");
DateUserControl2.SetM("7");
((DropDownList)DateUserControl2.Controls[4]).Text = "2003";
in DateUserControl.ascx.cs
i put all the listitems created in Page_Init and it works fine
the other methods
public void SetD(object d)
{
this.DropDownListDuc.Text = d + "";
}
public void SetM(object m)
{
this.DropDownListMuc.SelectedValue = m + "";
}
when i try to trace, i see that the methods are ok, but, for example,if d parameter is 4 and
this.DropDownListDuc.Text = 4 + "";
is performed, still NOTHING changes!!!
(again, the same line in a "simple" DDL,like DropDownList1.Text = "20"; changes the DDL to 20!!
changing a Label in the eser control works too. it is just a DDL_in_a_usercontrol problem
thanks!
Well, keep in mind that you are NOT allowed to have two controls with the same "id" on one page.
But, what then occurs if you drop the user control two times on the same web page?
I have a hotel edit UC.
So, I can do this:
Say this markup:
<uc1:UHotelEdit runat="server" ID="UHotelEdit"
MyTable ="tblHotelsA" />
<div style="clear:both;height:30px">
</div>
<uc1:UHotelEdit runat="server" ID="UHotelEdit2"
MyTable ="tblHotelsA" />
So, note we have TWO UC's on the page. UhotelEdit, and UhotelEdit2.
My page load code is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UHotelEdit.MyPk = 6;
UHotelEdit.LoadData();
UHotelEdit2.MyPk = 7;
UHotelEdit2.LoadData();
}
}
And now we have this:
So, in above, we have Hotel Name text box, first name, last name etc.
But, we are NOT allowed to have multiple "id" of the same control on the page.
So, how can/could I set the hotel name of the first control to Zoo, and Zoo2 for the 2nd one?
Well, all of the controls are "nested" inside of the UC.
This is quite much like a grid view, or whatever. So, you have two choices:
if several of the controls had to be "often" changed, then make a public property in the UC.
or, you can get/grab/pluck out the control name of the UC.
eg this:
protected void Button1_Click(object sender, EventArgs e)
{
TextBox txtHotel = UHotelEdit.FindControl("txtHotel") as TextBox;
TextBox txtHotel2 = UHotelEdit2.FindControl("txtHotel") as TextBox;
txtHotel.Text = "Zoo";
txtHotel2.Text = "Zoo2";
}
And thus we now have this:
But, if you had to "often" change the hotel name text box above?
Well, then add a public property to the UC, say like this:
public string HotelName
{
get { return txtHotel.Text; }
set { txtHotel.Text = value; }
}
And now our code becomes this:
protected void Button1_Click(object sender, EventArgs e)
{
UHotelEdit.HotelName = "Zoo";
UHotelEdit2.HotelName = "Zoo2";
}
So, for common or often controls to be changed? Setup a get/set for that control. For the rare and not common used?
Then you can use find control. Quite much you do the same to get a control out of repeater, listview, gridview etc. - you in general have to use find control for those nested controls.
Well, everything quite much works the same, but we have to prefix the control(s) with the name of the user control (id) we used when dropping on the page.
but, if you have JavaScript code, then how can I select the first one (hotel name) vs the 2nd one?
Turns out, all controls rendering inside of a UC will PREFIX the control with the name of the user control!!!
So, on the page you find two hotel text boxes,
UHotelEdit_txtHotel
UhotelEdit2_txtHotel
But, they are not exposed that way to code behind (just like controls nested in a listview or repeater also are not). but, they exist and jQuery selectors can be used against those controls.
In code behind, you have to use UC.findcontrol("txtHotel") to pluck out a reference to that control in the UC - since its nested inside.
So, if your UC has a dropdown list, and you want to with ease to change that value? Then add a PUBLIC method to the control that allows you do to this. So, while the controls inside are not exposed, you can with ease add a simple public method that allows you to change the drop down list value.
I am stuck with a problem which I have reduced to code below . I have two buttons on an aspx page.
Both the buttons have runat="server" property and are inside <form runat="server" > tag
btnGetData
protected void btnGetData_Click(object sender, EventArgs e)
{
headlines = masg.Split('*');
//Response.Write(headlines.Length);
cb = new CheckBox[headlines.Length];
for (int i = 0; i < headlines.Length; i++)
{
cb[i] = new CheckBox();
cb[i].Text = headlines[i];
Literal br = new Literal();
br.Text = "<br/>";
Form.Controls.Add(cb[i]);
Form.Controls.Add(br);
}
}
On clicking Get Data button , multiple checkboxes are generated with associated text .
I click on some of the checkboxes and then click on Show button which IF WORKS CORRECTLY should combine selected checboxes text into single string and display it.
btnShow
protected void btnShow_Click(object sender, EventArgs e)
{
for (int i = 0; i < headlines.Length; i++)
{
if (cb[i].Checked)
newmsg += cb[i].Text + '*';
}
Response.Write("<BR><BR><BR>" + newmsg);
}
But once I click on GetData button , the checkboxes are lost as they don't persist . I read about SESSION variables but can't figure out how to apply them .
I have declared below variables as global so that they can be accessed throughout the page.
CheckBox[] cb;
string[] headlines;
string masg;
Please help with code .Please provide me with inputs in code. I will refine my question if I am not able to make something clear.
You need to recreate dynamically created controls on every postback(in Page_Init or Page_Load) due to the statelessness of HTTP. You need to know what you have to recreate. Therefore you can save the number of already created CheckBoxes in a ViewState variable.
You only need to assign the same IDs as before and add them in Page_Load at the latest. If you know the number of controls to create you can derive the ID from the counter variable by appending it to the control-id.
Recommandable readings:
TRULY Understanding Dynamic Controls
Page-Lifecycle
Or you use one of the builtin Data-Bound Control like Repeater that do this automatically. You only have to set their DataSource and call DataBind().
Here are answers of me on similar questions with implementation details:
C#
VB.NET (+ C#)
The thing is had a button that would do that but i decided it to use an hyperlink, int the code you will see that the button i was using was redirecting me to the team page, in wich the id would give the information that would fill the other fields. Basically i am in the player page and i wantoi it to redirect to the the players team page by the id.
protected void Button4_Click(object sender, EventArgs e)
{
int playerId = Convert.ToInt32(Request.QueryString["Id"]);
DBLPlayers players = new DBLPlayers();
Player player = players.SelectById(playerId);
Response.Redirect("DetalhesClube.aspx?Id=" + player.Team.ID.ToString());
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
How can i use the hyperlink to do this?
Is just a matter of UI visualization? Use a LinkButton instead... the code behind will remain the same.
Do you prefer to have a direct HTML link to the new page? For each player load also the related team's Id in you aspx page and use it to compose the <a> hiperlink.
If you post the aspx code we could be more precise.
If you wont to use Button then set PostBackUrl property and you will save one server request, like this :
Button4.PostBackUrl = "DetalhesClube.aspx?Id=" + player.Team.ID.ToString();
But as I can see you don't wont to post any values just link to another page, so it will be better to use plain old href or HyperLink ASP.NET control, you can decorate it to look and act as a button trough css.
HyperLink1.NavigateUrl = "DetalhesClube.aspx?Id=" + player.Team.ID.ToString();
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.
hi I like to add Dynamically add WebUser Controls in a loop
like this con1 con2 con3 and more or less depending on the loop
is there a good way to do this
my first try look like this. but i don't know how to tell it to use the next one grpCon2
foreach (DataRow Group in AllGroups.Rows)
{
GroupListControl grpCon1 = new GroupListControl();
grpCon1.NickName = "STUFF";
grpCon1.GroupName = "HARD";
LiteralAddCOntrols.Text = #"<uc1:GroupListControl ID=""GrpCOn1"" runat=""server"" />";
}
You need to use loadcontrol(pathtoyourusercontrol), and then and the control back to your page at the location you want.
sharedUC uc = (sharedUC)LoadControl("~/sharedUC/control.ascx");
plcContent.Controls.Add(uc);
Add :
To the page aspx loading the control and you will be able to use a typed reference to it.
You can do that, but you have to remember two things:
You have to give them ID - and remember them in a Session
When the controls do any PostBack actions (like Click) - you have to refresh the exact collection on every post back in Page_PreInit event (which normaly the framework does) - because the attached event won't fire.
And the Page_PreInit have to refresh the exact collection with the same ID-s.
It's possible but it's not so simple at the beginning.
And here is a detailed description how to do that.
https://web.archive.org/web/20211031102347/https://aspnet.4guysfromrolla.com/articles/092904-1.aspx
You can use this way and use "updatePanel" to dynamically change your controllers:
here I use "userControls_DeviceController" as my Usercontroller Class name.
userControls_DeviceController FAN1;
userControls_DeviceController FAN2;
protected void Page_Load(object sender, EventArgs e)
{
FAN1 = (userControls_DeviceController)LoadControl("~/userControls/DeviceController.ascx");
saloon.Controls.Add(FAN1);
FAN2 = (userControls_DeviceController)LoadControl("~/userControls/DeviceController.ascx");
saloon.Controls.Add(FAN2);
}
and also for customizing your usercontrol you can put a timer on your page and use an updatepanel to change properties of the specify usercontrol.
protected void Timer1_Tick(object sender, EventArgs e)
{
int counter = Convert.ToInt32(Session["c"]);
FAN1.SetDeviceIndex(counter);//here I change usercontrol picture FAN1
FAN2.SetDeviceIndex(counter);//here I change usercontrol picture FAN2
counter++;
if (counter == 4)//I have 4 picture to changing.
{
counter = 0;
}
Session["c"] = counter;
UpdatePanel1.Update();
}
I hope it could help you ...