User control posts back but does not render - c#

I have one user control called calculator that is registered on a usercontrol (ektron widget). Two instances of these calculator controls are added to the widget in different panels. These panels make use of partial postbacks.
<asp:Panel runat="server" ID="pnlStep1">
<uc1:send_Calculator ID="stage1_Calculator" runat="server" />
</panel>
<asp:Panel runat="server" ID="pnlStep2">
<uc1:send_Calculator ID="stage2_Calculator" runat="server" />
The calculator control has a dropdownlist and a textbox.On the selectedindexchanged event all it does is change the value of a textbox i.e.
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{ tbExample.Text ="bla bla"; }
This functionality works for the first instance of the calculator control and the text changes but for the second instance of the control the event triggers so the postback has worked but the textbox does not change value. Its like it got lost somewhere on the way back to the client.
Any ideas why the one calc control would work and the other one wouldn't?

Related

How to set the background color for a user control button? and how to pass in parameters to its event handler?

I created a user control for a button because I will be needing to use it multiple times in the application.
<td class = "center" colspan = "4"><uc:Instructions ID="Instructions" runat="server" />
I have this in the ascx file of the button
<asp:Button ID ="btnInstrs" Text ="Instructions" runat ="server" CssClass ="buttonGreen" OnClick ="btnInstrs_onClick"/>
This is the code behind for the user-control
protected void btnInstrs_onClick(object sender, EventArgs e)
{
}
public void setColorGreen()
{
btnInstrs.BackColor = System.Drawing.Color.Green;
}
I have this as the CSS
.buttonGreen
{
background-color: green;
}
So far I have this in the code behind in one of the pages I will be needing this button
Instructions.setColorGreen();
The setColorGreen method is changing the background color to green when I call it. Any idea why using just the CSS isn't doing the job?
Now to my main question.
I want to return some instructions as a popup and some text on the page when the button is clicked. to get those instructions I will need an itemID to look in the DB and get the instructions So if I was doing everything in the source page. In the event handler I would make a D.B call for the item ID and find the instructions and display but because I want to do it using the user control I don't know how to pass in the itemID or anything else as the parameter into the user control event handler.
Any advise as to how I could code the event handler so I can re-use it in different pages to get the instructions(for an itemID) would be greatly appreciated.
Calling a method in parent page from user control
This answers my second question. Create an event for the user control when clicked. Handle the event in the source page when exposed.
I can use my itemID in the event handler to make necessary database calls and retrieve information and publish the instructions.
I do the same thing for any other pages required.
Still did not understand why CSS isn't setting the background color of the button to Green.

OnCheckedChanged not firing when clicked

I'm trying to run my OnCheckedChanged inside an itemtemplate, but it is not firing. What I did was I typed the OnCheckChanged in the asp:CheckBox tag and also typed the entire method manually. Would this affect the process??
<asp:CheckBox runat="server" ID="uoCheckBoxTagtoVehicle" OnCheckedChanged="ChkChanged" AutoPostBack="true" Width="50px" />
and my event:
protected void ChkChanged(object sender, EventArgs e)
{
uoHiddenFieldVehicle.Value = "1";
}
Note: I'm using Visual studio 2008
Since your control is inside a GridView (since you said ItemTemplate I assume you do) you can't use your approach to attach the event as you did. Because there will be multiple check boxes once you populate the GridView. Therefore, do the following
In you GridView's DataBinding event find the CheckBox by ID (use FindControl method)
Then attach the event OnCheckedChanged to the method you've written
Maybe you are databinding the page also on postback. You should do that only ...
if(!IsPostBack)
{
DataBindPage(); // method which databinds your controls like GridView
}
Otherwise you prevent that events are triggered.

Why DropDownList event fires after another event

I have a DropDownList in my page:
<asp:DropDownList ID="ddlPra" ClientIDMode="Static" CssClass="chosen-select" runat="server" OnSelectedIndexChanged="Practice_SelectedIndexChanged"></asp:DropDownList>
Code Behind:
protected void Practice_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(ddlCli.SelectedItem.Text);
}
I also have an UpdatePanel which has a GridView that displays some data with links. The issue I am having is when the page loads and I change the option from the select drop down, I don't get an alert but when I click on anything inside theUpdatePanel` the alert is displayed.
How do I fix it so that the DropDownList works independent from the UpdatePanel
Why DropDownList event fires after another event?
Because you have not set AutoPostBack="true", it is false by default. That means that it will not post back immediately after the user selected another item. But the event will be triggered on the next postback anyway, independent of the control that caused it.

Button click event not firing within use control in ASP .Net

I am developing an asp web page in which I have a drop down combobox and a place holder below that. When the user selects an item from the drop down combobox, a postback is done to the server side and server loads an asp user control to the place holder in this parent page. Everything upto now is working fine.
In the user control I have a button and the user control code behind is implemented to handle the button click event. The problem is, when I click this button, I can see that the postback is send to the server side (i.e. parent page Page_Load() is invoked in debug mode), but both the user control's Page_Load() or button click event handler is not invoked.
Please help..
Some additional information,
My parent page is not an asp master page. Just a simple asp page.
I am using VS2008 and .Net 3.5 SP1 and C#.
You need to ensure that you UserControl exists so the button click event is triggered when viewstate is rebuilt.
Loading your UserControl in the Page_Load will work the first time. When you click the button and the post_back occurs, Page_Load has not occurred yet. This means the UserControl will not exist, which mean the button does not exist for the event to be wired back up. So the UserControl with the button in it cannot be connected to the click event and the click event wont fire.
Recommend that your user control is loaded in this event.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//-- Create your controls here
}
Try a sandbox test. In page_load, dynamically create a button with a click event in the Page_Load. You will see that the click event does not fire. Now move the button to the OnLoad event. The click event will fire. Also note, the click event will occur before the Page_Load event. Further proof that the button does not exist at the right time.
Another idea...
You are reloading the usercontrol on the page before the button event occurs. Ensure your LoadControl method is inside the If block
if (!IsPostBack)
{
//load usercontrol
}
Default.aspx
<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
var ctl = LoadControl("Controls/UserControl.ascx");
ph1.Controls.Add(ctl);
}
UserControl.ascx
<h3>User control</h3>
<asp:Button ID="btn1" runat="server" OnClick="btn1_Click" Text ="Click me" />
UserControl.ascx.cs
protected void btn1_Click(object s, EventArgs e)
{
Response.Write("You clicked me, yay");
}
All works like a charm. I see the "You clicked me, yay" written when I click the button
Point of attention. If you try to load the controls dynamically in your example in the handler for SelectedItemChanged event of the dropdown control, it will fail, because of the way that lifecycle works for ASP.Net page.
Instead you should handle such control creation in the PageLoad event of the page, like this example below
Default.aspx
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true">
<asp:ListItem Value="0" Text="--select a value--" />
<asp:ListItem Value="1" Text="User control 1" />
<asp:ListItem Value="2" Text="User control 2" />
</asp:DropDownList>
<asp:PlaceHolder runat="server" ID="ph1">
</asp:PlaceHolder>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
switch (ddl1.SelectedValue)
{
case "1":
var ctl = LoadControl("Controls/UserControl.ascx");
ph1.Controls.Add(ctl);
break;
case "2":
ctl = LoadControl("Controls/UserControl2.ascx");
ph1.Controls.Add(ctl);
break;
}
}
}
In my particular case, I found that the problem was the UserControl ID (or rather the lack of).
When the UserControl was first instantiated, my button ID was ctl00$ctl02$btnContinue, but after the postback it had changed to ctl00$ctl03$btnContinue, therefore the button event handler didn't fire.
I instead added my UserControl with a fixed ID and the button now always loads with the ID ctl00$myUserControl$btnContinue.

Order of Postback event handler and Validate method of all validator controls

I'm learning asp.net page life cycle.
I find an article on MSDN
http://msdn.microsoft.com/en-us/library/ms178472.aspx
It says that
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
And the picture in the article also says validate fires after event handling.
If this is true how can I get status of validator when I handling the event?
In further confirmation of Ben Robinson's answer, it looks like Microsoft updated the documentation after this question was asked. The MSDN article clarifies:
If the request is a postback, control event handlers are called. After
that, the Validate method of all validator controls is called, which
sets the IsValid property of individual validator controls and of the
page. (There is an exception to this sequence: the handler for the
event that caused validation is called after validation.)
This is the correct behavior. You can test this with a simple page:
Markup:
<form id="form1" runat="server">
<asp:TextBox ID="txtTest" runat="server" ValidationGroup="test" OnTextChanged="txtTest_TextChanged" />
<asp:CustomValidator ID="cvTest" runat="server" ValidationGroup="test" ControlToValidate="txtTest" ValidateEmptyText="true" OnServerValidate="txtTest_Validate" />
<asp:Button ID="btnTest" runat="server" Text="Test" ValidationGroup="test" OnClick="btnTest_Click" />
</form>
Code-behind:
protected void txtTest_TextChanged(object sender, EventArgs e)
{
}
protected void txtTest_Validate(object sender, ServerValidateEventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
}
Set breakpoints on all the events. Run the app, change the text in the box, and click the button. You should observe that TextChanged fires first, then Validate, then Click.
Validators are validated before their server side event handlers are fired, you can rely on e.IsValid in them, this definitely works, many people use it all the time.
I'm not certain the picture is correct, as by the time you are in the click event of a button, the validators have fired. You call Page.IsValid to check and see if any validators have failed.
I've never used it, but the Page class also contains a collection of validators (Page.Validators). You might be able to use that to determine which specific validators are failing.
Page_Load event fires before the Control's Server Event. You can validate the Page with Page.IsValid in Page_Load event and leave the Page Cycle to handle the rest. This is commonly used in most of the projects.

Categories