AutoPostBack= "true" not functioning properly with dropdown list asp.net - asp.net

I have a dropdown list that is populated with values from a DB table, when you select a value from the list a repeater appears and shows extra details about that selected value. The only problem is that the page only refreshes for the first value selected, if you try to make a different selection the page doesn't change.
The repeater seems to be working fine, but there must be something wrong with the AutoPost back in the dropdown.
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="LCID" DataValueField="LCID" EnableViewState="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack= "true" >
</asp:DropDownList>
this is the code from the aspx.cs file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("http://localhost:31003/?LCID="+ DropDownList1.SelectedValue);
}
}
If anyone can see what I am missing from my program I would be very grateful, thanks.

you are missing the PageName in Response.Redirect
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("~/PageName.aspx?LCID="+ DropDownList1.SelectedValue);
}

Related

Textbox Text change Event

I have a form and I am want to call a stored procedure on text change event of that text-box ,
Can you suggest any idea.
Thanks in advance.
You just have to handle the TextChanged-event:
<asp:TextBox ID="TextBox1" runat="server"
OnTextChanged="TextBox1_TextChanged">
</asp:TextBox>
Codebehind:
protected void TextBox1_TextChanged(Object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
// call your stored-procedure
}
Note that you should databind the controls only if(!IsPostBack), otherwise the events are not triggered. So for example in Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindForm(); // which sets the `Text` property of this TextBox among others
}
}

Dropdownlist in gridview not firing selectedindex changed event

I have problem with not firing selected index changed event of dropdownlist in gridview. I gone through the SO Thread . It is not worked wholly for me. I have implementation like below.
.ASPX
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL1_SelectedIndexChanged">
<asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
<asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
</asp:DropDownList>
.CS
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}
protected void grdPoll_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(Page.IsPostBack)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DDL1") as DropDownList;
if(ddl != null)
{
ddl.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
}
}
}
}
When i keep if(!Page.IsPostBack) block only then it works fine. But i want else block also. Whats going wrong with implentation. Can you please suggest the solutions
The problem is block after !Page.IsPostBack block, which is not event else part as you said. You are binding grid again on post back which results in loss of the event being fired. You do not have to bind it again to have the changes in the grid.
Remove this code.
{
// Bind the GridView again to maintain previous entered data in the gridview
DataBindGrid();
}
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
// Bind the GridView to something.
DataBindGrid();
}
else {
// Bind the GridView again to maintain previous entered data in the gridview
//DataBindGrid(); //remove DataBindGrid(); from else
}
}
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblCity.Text = ((DropDownList)sender).SelectedValue;
DataBindGrid();
}
replace event name "Page_Load" with "Page_PreRender"

what happens when an web server control on an .aspx file is redefined in the code behind?

Mark Up:
<asp:Label ID="Status" runat="server" Visible="false" />
Code Behind:
public partial class Files : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Status;
protected void Page_Load(object sender, EventArgs e)
{
Status.
}
}
Now whenever I tried to use the label Status in the Page_Load handler I was warned as this member is defined more than once. My question is why I was not warned while redifining it as an instance member ? and actually is it possible to proceed with the control ?
You do not need this line:
protected System.Web.UI.WebControls.Label Status;
because when you declare a control in ASP.net layout it is automatically created as a field of your page class.
You have already used the Label Object Status in Designer. Can you check the designer .cs class for it's declaration? A variable with the same name can not be declared twice.
Corrected code is below...
Mark Up:
<asp:Label ID="Status" runat="server" Visible="false" />
Code Behind:
public partial class Files : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Pseudo Code
//Status.PropertyName....
}
}

Ajax CalendarExtender, How to get the date after i click on a button?

I want to add add an Ajax CalendarExtender to my page. And then after selecting a date and clicking on a button I get the selected Day in a label.
I have a text box which is the target of the CalendarExtender
<asp:TextBox ID="DateText" runat="server" ReadOnly="true" ></asp:TextBox>
<ajaxToolkit:CalendarExtender
ID="Calendar1"
runat="server"
TargetControlID="DateText"
Format="MMMM d, yyyy"
PopupPosition="Right"
/>
<asp:Button runat="server" ID="Button1" onclick="Button1_Click" />
In the Code Behind:
On the first page load I set the date to Today.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.SelectedDate = DateTime.Today;
}
}
In the Button1_Click Event
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Calendar1.SelectedDate.Value.Day.ToString();
}
The problem is when I click the button (or after any post backs) the value selected get reset to Today's date.
And if I don't set it to DateTime.Today in the Page_Load It get reset to null and throw a null exception.
How can I solve this?
Thank you very much for any help.
I hope i was clear
Maybe this will work:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(!Calendar1.SelectedDate)
Calendar1.SelectedDate = DateTime.Today;
}
}
The solution to the issue is making use of Request.Form collections. As this collection has values of all fields that are posted back to the server and also it has the values that are set using client side scripts like JavaScript.
Thus we need to do a small change in the way we are fetching the value server side.
C#
protected void Submit(object sender, EventArgs e)
{
string date = Request.Form[txtDate.UniqueID];
}

Am I supposed to be able to alter other controls from an ASP.NET page event handler?

I'm trying to disable a label control from the CheckedChanged event handler of a checkbox. Should I be able to do this?
At the moment if I set Enabled to false nothing changes when the page reloads. If I do the same thing in Page_Load then I see the change.
To clarify:
This doesn't work:
protected void chkNeverExpires_CheckedChanged(object sender, EventArgs e)
{
this.lblMessage.Enabled = false
}
But this does:
protected void Page_Load(object sender, System.EventArgs e)
{
this.lblMessage.Enabled = false
}
Are you sure you're events are firing in the order you expect? Put breakpoints on all your postback methods and watch what happens, are you resetting the enabled state anywhere? do you have enableviewstate=false on anything?
Edit: You realise CheckedChanged doesnt fire until you postback from another control, or you have AutoPostBack=true on the checbkbox?
This works fine:
<asp:Label runat="server" ID="lblTest">test</asp:Label>
<asp:CheckBox runat="server" ID="chkCheck" AutoPostBack="true" />Check
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
chkCheck.CheckedChanged += chkCheck_CheckedChanged;
}
private void chkCheck_CheckedChanged(object sender, EventArgs e)
{
lblTest.Enabled = false;
}

Resources