I have a DropDownList and I want to check what language does the browser have and set the values in the dropdown accordingly.
protected void Page_Load(object sender, EventArgs e)
{
string language = Request.UserLanguages[0].ToString().Substring(0, 2);
drpAnrede.DataSource = Server.MapPath("~/App_Data/" + language + ".xml");
}
UPDATE:
I have the solution for this problem...
aspx:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="drpAnrede" runat="server" DataTextField="display" DataValueField="id">
</asp:DropDownList>
<asp:XmlDataSource ID="xmldata" runat="server"></asp:XmlDataSource>
</div>
</form>
c#:
protected void Page_Load(object sender, EventArgs e)
{
string language = Request.UserLanguages[0].ToString().Substring(0, 2);
//drpAnrede.DataSource = Server.MapPath("~/App_Data/" + language + ".xml");
xmldata.DataFile = Server.MapPath("~/App_Data/" + language + ".xml");
drpAnrede.DataSourceID = xmldata.ID;
}
You have to call DataBind() on your dropdown list after you set the datasource, no?
As in:
Databinding DropDown Control in .Net
Assuming the XML is Ok, you need to call
drpAnrede.DataBind();
after applying the datasource.
Related
This is my front End Code in asp.net form
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<span id="Message" runat="server"></span>
<span id="myid" runat="server">
</span>
</asp:Content>
here is my code behind
protected void Page_Load(object sender, EventArgs e)
{
myid.InnerHtml = myid.InnerHtml + "<asp:Button ID=" + dbLayer.AddDoubleQuotes("Button_Update") + " name=" + dbLayer.AddDoubleQuotes("Button_Update") + " runat=" + dbLayer.AddDoubleQuotes("server") + " class=" + dbLayer.AddDoubleQuotes("btn btn-success") + " OnClick=" + dbLayer.AddDoubleQuotes("Button_Update_Click") + " Text=" + dbLayer.AddDoubleQuotes("UpdateButton") + " />";
}
protected void Button_Update_Click(Object sender, EventArgs e)
{
Message.InnerHtml = "This Event Fired Sucessfully ...";
}
i am using web Method for Double Quotes
[WebMethod]
public string AddDoubleQuotes(string value)
{
return "\"" + value + "\"";
}
Not Able to get button click event
your reply helps me lot, i am stuck due to this
First of all, you cannot just simply render Server Control as string literal in ASP.Net Web Form.
Dynamically added server controls are a little bit tricky since they are not in control tree. You need to reload them back with same ID inside either Page_Init or Page_Load event.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
<asp:Label runat="server" ID="MessageLabel" />
protected void Page_Init(object sender, EventArgs e)
{
var button = new Button {ID = "Button1"};
button.Click += Button_Click;
PlaceHolder1.Controls.Add(button);
}
private void Button_Click(object sender, EventArgs e)
{
MessageLabel.Text = "Button is clicked!";
}
FYI: Use ASP.Net Server control as much as possible instead of regular html control with runat="server", unless you absolutely certain that you do not need ViewState and some extra features offered by those server controls.
If EnableViewState is false, you can repopulate placeholder controls with different controls dynamically on a postback. The ids do not matter then.
I have a WebForms page that I would like to inject some additional controls into at runtime. Currently I am achieving this in the Page_Load event using a Literal control.
For example the page looks like this (note that the TextBox1 is not an asp control just to show that it works):
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<input id="TextBox1" type="text" runat="server"/>
<asp:Literal ID="Literal1" runat="server" Visible="false"></asp:Literal>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:Content>
And the code behind:
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Visible = true;
if (!IsPostBack) Literal1.Text = "<input id=\"TextBox2\" type=\"text\" runat=\"server\"/>";
}
This works fine and both textboxes appear on the screen but if I type a value in to both and trigger a postback only the value of TextBox1 is retained.
I have tried moving my code to OnPreRender and OnPreLoad but still have the same issue.
I have noticed that when I view the page source TextBox1 has a UniqueId (e.g. ctl00$MainContent$TextBox1) while Textbox2 still has runat="server" as an attribute.
You can't inject server controls like this. You would need to add them as suggested in #Arvin's answer.
However, you use inject non-ASP.NET HTML controls similar to what you are doing and get their values.
From your code change the input's id to a name and drop the runat="server":
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Visible = true;
if (!IsPostBack) Literal1.Text = "<input name=\"TextBox2\" type=\"text\" />";
}
Then you can get it's value on postback:
string textbox2value = Request.Form["TextBox2"];
Then, if you want to add the control on postback with it's value:
Literal1.Text = "<input name=\"TextBox2\" type=\"text\" value=\"" +
Server.HTMLEncode(textbox2value) + "\" />";
if you want to inject a textbox you should use placeholder like this :
<input id="TextBox1" type="text" runat="server"/>
<asp:PlaceHolder ID="plh" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Page_Load(object sender, EventArgs e)
{
TextBox TextBox = new TextBox();
TextBox.ID = "TextBox2";
plh.Controls.Add(TextBox);
}
protected void Button1_Click(object sender, EventArgs e)
{
var Text1 = TextBox1.Value;
var Text2 = Request.Form["TextBox2"];
}
I have a dropdown in which I am adding list items dynamically. I set autopostback to true, but nothing seems to happen when I select an item in dropdown.
Mark Up
`<asp:DropDownList runat="server" AutoPostBack="true" ID="restaurant_city_con" CssClass="selectboxindex"></asp:DropDownList>`
Code Behind
`if (!this.IsPostBack)
{
addStates();
showData();
dashboardPageFunction();
ordersPageFunction();
reportsPageFunction();
categoriesPageFunction();
menuPageFunction();
offersPageFunction();
bookingPageFunction();
}
else
{
addCities();
addZipCode();
}`
Is there anything I am doing wrong ?
You need to handle the OnSelectedIndexChanged event, like this:
Markup:
<asp:DropDownList runat="server" AutoPostBack="true" ID="restaurant_city_con"
CssClass="selectboxindex"
OnSelectedIndexChanged="restaurant_city_con_SelectedIndexChanged">
</asp:DropDownList>
Code-behind:
protected void restaurant_city_con_SelectedIndexChanged(object sender,
EventArgs e)
{
// Do something with selected item here
Label1.Text = "You selected " + restaurant_city_con.SelectedItem.Text +
" with a value of " + restaurant_city_con.SelectedItem.Value +
".";
}
I want to know that how can I trace out the value of Textbox from ViewState.
As user enters any value into Textbox and click submit button because of postback Textbox value disappears ,
But if I used ViewState in this case , then is there any way to see or display that value from Viewstate?
<html>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text += "X";
}
In your page load use this.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ViewState["Values"] == null)
{
ViewState["Values"] = new string();
}
}
TextBox1.Text = ViewState["Values"].ToString();
}
After that use this.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Values"] += TextBox1.Text;
}
In the first Page_Load method, You will create a ViewState if its not a postback and its null. After that write the textbox your viewstate, in Button1_Click you will add your new textbox1 to your viewstate.
The OnSelectedIndexChanged event is not firing for my dropdown box. All forums I have looked at told me to add the AutoPostBack="true", but that didn't change the results.
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Current Time: " /><br />
<asp:Label ID="lblCurrent" runat="server" Text="Label" /><br /><br />
<asp:DropDownList ID="cboSelectedLocation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboSelectedLocation_SelectedIndexChanged" /><br /><br />
<asp:Label ID="lblSelectedTime" runat="server" Text="Label" />
</div>
</form>
</body>
</html>
Code behind:
public partial class _Default : Page
{
string _sLocation = string.Empty;
string _sCurrentLoc = string.Empty;
TimeSpan _tsSelectedTime;
protected void Page_Load(object sender, EventArgs e)
{
AddTimeZones();
cboSelectedLocation.Focus();
lblCurrent.Text = "Currently in " + _sCurrentLoc + Environment.NewLine + DateTime.Now;
lblSelectedTime.Text = _sLocation + ":" + Environment.NewLine + DateTime.UtcNow.Add(_tsSelectedTime);
}
//adds all timezone displaynames to combobox
//defaults combo location to seoul, South Korea
//defaults current location to current location
private void AddTimeZones()
{
foreach(TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
{
string s = tz.DisplayName;
cboSelectedLocation.Items.Add(s);
if (tz.StandardName == "Korea Standard Time") cboSelectedLocation.Text = s;
if (tz.StandardName == System.TimeZone.CurrentTimeZone.StandardName) _sCurrentLoc = tz.StandardName;
}
}
//changes timezone name and time depending on what is selected in the cbobox.
protected void cboSelectedLocation_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
{
if (cboSelectedLocation.Text == tz.DisplayName)
{
_sLocation = tz.StandardName;
_tsSelectedTime = tz.GetUtcOffset(DateTime.UtcNow);
}
}
}
}
Any advice into what to look at for a rookie asp coder?
EDIT: added more code behind
Graham Clark was correct in needing the !Page.IsPostBack, but it is now something with the global variables that I set. This code was dragged and dropped from a c# project, so I assume there is some issues with global variables and asp.net. Time for me to do more research on this to understand how global variables differ in a standalone as opposed to a web program.
Are you databinding your drop-down list every trip back to the server, or just on a postback? If you're doing it every time, it could be that the server doesn't think anything has been selected, therefore the event won't fire.
Say you're databinding the drop-down in the Page_Load event. You want to do it like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// bind drop-down list here
}
}