ASP.Net dropdown control loses state after being set dynamically through javascript - asp.net

I have an asp.net dropdownlist control. There are two ways it can be selected.
The first is through a button that sets the value in the drop-down list via javascript.
The second is by manually selecting the dropdownlist option.
Each of these methods work by themselves.
If I do the first followed by the second and then hit save - the value that is saved is the value set by the javascript. The manual selection did not have any effect.
Does anyone know why this is the case?
EDIT:
In the HTML Head section:
<script>
function Select(allocationId) {
document.getElementById('Accounts').value=allocationId;
}
</script>
In the HTML Body:
<asp:DropDownList ID="Accounts" runat="server"></asp:DropDownList>
<button onclick="Select(<%#"'" + DataBinder.Eval(Container.DataItem, "Associated_AccountId") + "'"%>)" type="button">Select</button>
<asp:Button ID="btn_SaveAndClose" runat="server" Text="Save and Close" OnClick="btn_SaveAndClose_Click"></asp:Button>
In the code behind:
protected void btn_SaveAndClose_Click(object sender, System.EventArgs e)
{
int id = Convert.ToInt32(this.Accounts.SelectedValue);
}
EDIT:
Funnily enough - when I use:
int id = Convert.ToInt32(Request.Form["Accounts"]);
it works. But I don't know why.

The most common mistake that would cause this is that you're likely populating or binding that DropDownList every Page_Load or Init, thus erasing whatever state you entered the page with. Be sure to do a (!IsPostBack) check before populating your DropDownList.
This worked for me: (try removing the !IsPostBack check and see if the behavior is similar to what you're seeing in your app)
page content
<asp:DropDownList ID="ddlTest" runat="server"></asp:DropDownList>
<button onclick="select(3);">Select Three</button>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
<script type="text/javascript" language="javascript">
//<![CDATA[
function select(value) {
var ddlTest = document.getElementById('<%=ddlTest.ClientID%>');
for (var i = 0; i < ddlTest.options.length; i++) {
if (ddlTest.options[i].value == value) {
ddlTest.selectedIndex = i;
return;
}
}
}
//]]>
</script>
<asp:Label ID="lblResult" runat="server"></asp:Label>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
PopulateDropDown();
}
protected void btnSave_Click(object sender, EventArgs e)
{
lblResult.Text = ddlTest.SelectedValue;
}
void PopulateDropDown()
{
Dictionary<int, string> data = new Dictionary<int, string>();
data.Add(1, "One");
data.Add(2, "Two");
data.Add(3, "Three");
ddlTest.DataSource = data;
ddlTest.DataTextField = "Value";
ddlTest.DataValueField = "Key";
ddlTest.DataBind();
}

Related

Snackbar in ASP.NET

I am using this code from w3schools:
TryIt
How can i integrate it with asp:LinkButton or asp:Button using OnClientClick, while onClick is executing the server code.
Almost a year late to the party, but I've just implemented the same snackbar.
It needs an UpdatePanel and you need to register a client script block on your event handler:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<div>
<asp:Button runat="server" ID="ShowSnackbar" Text="Show Snackbar" OnClick="ShowSnackbar_Click" />
</div>
<div id="snackbar">
<asp:Label runat="server" ID="Snack" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
The codebehind was tricky but it works. On page load, create a string that contains the javascript, then use that string value to register your script block...
private string snackbarScript;
protected void Page_Load(object sender, EventArgs e)
{
snackbarScript = GenerateSnackbarJS();
}
private string GenerateSnackbarJS()
{
var sb = new StringBuilder();
sb.AppendLine("var x = document.getElementById('snackbar');");
sb.AppendLine("x.className = 'show';");
sb.AppendLine("setTimeout(function(){ x.className = x.className.replace('show', ''); }, 3000);");
return sb.ToString();
}
protected void ShowSnackbar_Click(object sender, EventArgs e)
{
Snack.Text = "Here's the snackbar";
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "snackbar", snackbarScript, true);
}

How to hide a div using an ASP.NET Checkbox?

i'm trying to do this assigning an method to the event on the checkbox tag like this:
OnCheckedChanged="ShowDiv"
However, i'm not succesfull at all. How to do it? Here's my method:
public void ShowDiv(object sender, System.EventArgs e)
{
var div = FindControl("ListaEmOutrosDocumentos") as HtmlGenericControl;
var checkbox = FindControl("Principal") as CheckBox;
if(checkbox.Checked == true)
{
div.Style.Clear();
div.Style.Add("display","block");
}
else
{
div.Style.Clear();
div.Style.Add("display","none");
}
}
You might consider using an ASP.NET Panel control instead of a div. That will have a Visible property that you can set.
Quick way using server side code only
Aspx
<div id="myDiv" runat="server" style="height:200px;width:100px;background-color:Blue"></div>
<asp:CheckBox ID="chkShowHideDiv" runat="server" AutoPostBack="True"
oncheckedchanged="chkShowHideDiv_CheckedChanged" Text="Hide Div" />
Code behind
protected void chkShowHideDiv_CheckedChanged(object sender, EventArgs e)
{
myDiv.Visible = chkShowHideDiv.Checked ? false : true;
}
I did this and it worked.
<div id="ListaEmOutrosDocumentos" runat="server">
<asp:CheckBox runat="server" ID="Principal" AutoPostBack="True" OnCheckedChanged="ShowDiv"/>
</div>
Code Behind
public void ShowDiv(object sender, System.EventArgs e)
{
var div = ListaEmOutrosDocumentos as HtmlGenericControl;
var checkbox = sender as CheckBox;
if (checkbox.Checked == true)
{
div.Style.Clear();
div.Style.Add("display", "block");
}
else
{
div.Style.Clear();
div.Style.Add("display", "none");
}
}

C# - Event handler for dynamic buttons

I have a textbox and submit button created using the design mode.
When the submit button is pressed, it will retrieve the user input from the textbox and then make a query to my database.
It will then display a list of dynamic buttons according to the information retrieved from my database.
However, the event handler for the buttons does not fire when clicked. I guess my problem is the postback but I cannot create those buttons in page_load etc. because I need to get the user input (from the textbox when submit button is pressed) before i can load the buttons.
How can i solve this problem?
Thank you.
Edit (codes):
protected void subBtn_Click(object sender, EventArgs e)
{
//database setup codes
.......
while (reader.Read())
{
Button detailsBtn = new Button();
detailsBtn.Text = reader["fName"].ToString();
//doesn't fire
detailsBtn.Click += new EventHandler(detailsBtn_Click);
memPanel.Controls.Add(detailsBtn);
}
}
Main problem is Postback regenerate dynamic controls on each postback if those controls does not exists.
For quick demo see this code
ASPX CODE
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</form>
ASPX.CS CODE
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
generate();
}
}
public void generate()
{
if (!pnl.HasControls())
{
for (int i = 0; i < 4; i++)
{
Button detailsBtn = new Button();
detailsBtn.Text = "fName" + i.ToString();
detailsBtn.ID = i.ToString();
detailsBtn.Click += new EventHandler(detailsBtn_Click);
pnl.Controls.Add(detailsBtn);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
generate();
}
protected void detailsBtn_Click(object sender, EventArgs e)
{
}
Sound to me like you could easily refactor your page to use a simple <asp:Repeater runat="server" ..></asp:Repeater> instead of dynamically adding controls to a Panel.
Here is a very simple complete sample:
RepeaterTest.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:TextBox ID="theTextBox" runat="server"></asp:TextBox>
<asp:Button ID="theButton" runat="server" OnClick="theButton_Click" Text="Click me" />
<asp:Repeater ID="test" runat="server">
<ItemTemplate>
<asp:Button ID="theRepeaterButton" runat="server" Text='<%# Eval("fName") %>' OnClick="theRepeaterButton_Click" />
</ItemTemplate>
</asp:Repeater>
</asp:Content>
RepeaterTest.aspx.cs
public partial class RepeaterTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void theButton_Click(object sender, EventArgs e)
{
string filter = theTextBox.Text;
// below row simulates fetching data using the filter text in the text box
var data = Enumerable.Range(0, 20).Select(i => new { fName = filter + " " + i });
test.DataSource = data;
test.DataBind();
}
protected void theRepeaterButton_Click(object sender, EventArgs e)
{
var button = (Button)sender;
// do something here based on text/commandname/commandargument etc of the button
}
}

Radio button doesn't get selected after a post back

I have an item template within repeater:
<ItemTemplate>
<li>
<input type="radio"
value="<%# GetAssetId((Guid) (Container.DataItem)) %>"
name="AssetId"
<%# SelectAsset((Guid) Container.DataItem) %> />
</li>
</ItemTemplate>
I have a method that compares ids and decides whether to check the radio button.
protected string SelectAsset(Guid uniqueId)
{
if (uniqueId == GetSomeId())
return "checked=\"checked\"";
return string.Empty;
}
SelectAsset gets hit, but it doesn't select a radio button on a post back, but it does work if I just refresh the page. What am I doing wrong here?
Answer here: How to display "selected radio button" after refresh? says that it's not possible to achieve, is this really the case?
Thank you
Update
It appears that view state isn't available for simple controls if they don't have a runat attribute. I have solved this by using a custom GroupRadioButton control. Thank you for your help.
I'd suggest using a RadioButtonList:
Page Code
<asp:RadioButtonList RepeatLayout="UnorderedList" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true" ID="RadioRepeater" runat="server" />
<asp:Label ID="SelectedRadioLabel" runat="server" />
Code Behind
if (!Page.IsPostBack)
{
/* example adds items manually
- you could iterate your datasource here as well */
this.RadioRepeater.Items.Add(new ListItem("Foo"));
this.RadioRepeater.Items.Add(new ListItem("Bar"));
this.RadioRepeater.Items.Add(new ListItem("Baz"));
this.RadioRepeater.SelectedIndex = this.RadioRepeater.Items.IndexOf(new ListItem("Bar"));
this.RadioRepeater.DataBind();
}
protected void IndexChanged(object sender, EventArgs e)
{
this.SelectedRadioLabel.Text = string.Format("Selected Item Text: {0}", this.RadioRepeater.SelectedItem.Text);
}
I assume you only need to select one item.
As described in the comments, it even works to access the SelectedItem in the Page_Loadevent handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// previous code omitted
}
else
{
string foo = this.RadioRepeater.SelectedItem.Text;
}
}
If you are creating all your controls dynamically at run-time (directly from code), then things are a little different. Here is the code that I used:
Page Code
<form id="form1" runat="server">
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += rbl_SelectedIndexChanged;
rbl.Items.Add("All");
// generate your dynamic radio buttons here
for (int i = 0; i<5; i++)
{
rbl.Items.Add(string.Format("Dynamic{0}", i));
}
form1.Controls.Add(rbl);
if (!Page.IsPostBack)
{
rbl.SelectedValue = "All";
PopulateTextBox(rbl.SelectedValue);
}
}
void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList foo = (RadioButtonList)sender;
PopulateTextBox(foo.SelectedValue);
}
void PopulateTextBox(string selection)
{
TextBox box = new TextBox();
box.Text = selection;
form1.Controls.Add(box);
}

asp.net editing data

I'm reading data from database and showing it in a page for editing:
<h2>Create new topic:
<asp:Label ID="_lblTopicName" runat="server" Text=""></asp:Label></h2>
<p>
Edit Level:
<br/>
<asp:DropDownList ID="_dtlEditRole" runat="server"></asp:DropDownList>
<br/>
View Level:
<br/>
<asp:DropDownList ID="_dtlViewRole" runat="server"></asp:DropDownList>
<br/>
<asp:TextBox ID="_tbxTopicText" TextMode="MultiLine" runat="server" Height="204px"
Width="885px"></asp:TextBox>
</p>
<asp:Button ID="_btnSaveTopic" runat="server" Text="Save" onclick="_btnSaveTopic_Click" />
I fill the fields in Page_PreRender() like so:
private string _topicString;
private Topic _topic = null;
private Topics_GetTopicByTopicResult _findTopicResults = null;
protected void Page_PreRender(object sender, EventArgs e)
{
// Load the User Roles into checkboxes.
_dtlEditRole.DataSource = Roles.GetAllRoles();
_dtlEditRole.DataBind();
_dtlViewRole.DataSource = Roles.GetAllRoles();
_dtlViewRole.DataBind();
_topicString = Request.QueryString["Topic"];
if (String.IsNullOrEmpty(_topicString))
{
Response.Redirect("~/Default.aspx");
}
else
{
_topic = new Topic();
_findTopicResults = _topic.FindTopic(_topicString);
if (_topic != null)
{
// Check if the user has permission to access
if (RoleHelper.IsEditAllowed(_findTopicResults.ViewRoleName))
{
_lblTopicName.Text = _findTopicResults.Topic;
_tbxTopicText.Text = _findTopicResults.Text;
_dtlEditRole.SelectedValue = _findTopicResults.EditRoleName;
_dtlViewRole.SelectedValue = _findTopicResults.ViewRoleName;
}
else
{
Response.Redirect("~/Error.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl));
}
}
else
{
Response.Redirect("~/CreateTopic.aspx?Topic=" + _topicString);
}
}
}
But now when i click _btnSaveTopic button the fields:
private string _topicString;
private Topic _topic = null;
private Topics_GetTopicByTopicResult _findTopicResults = null;
They are all NULL and im not able to update aything.
Here's my button click event:
protected void _btnSaveTopic_Click(object sender, EventArgs e)
{
_topic.UpdateTopic(_findTopicResults.ID, _findTopicResults.Topic, _tbxTopicText.Text,
_dtlViewRole.SelectedItem.Text, _dtlEditRole.SelectedItem.Text);
Response.Redirect("~/ViewPage.aspx?Topic=" + _topicString);
}
What would be the right way doing this?
ASP.NET Page Life Cycle states that Page_Init should be used to 'initialize control properties' which looks like what you are doing.
Also, it's usually good practice to breakup such large sections of code into smaller refactored methods. Try to keep the amount of code directly placed in event handlers to a minimum.
You can start by right-clicking a section of highlighted code in visual studio -> refactor -> extract method
Also, if you need more help understanding how to improve your code, you should ask a question pointing to this question on the code review site: here
You are re-binding the drop down list (and therefore wiping out the 'SelectedValue') in your Page_PreRender method. Wrap the method in
protected void Page_PreRender(object sender, EventArgs e)
{
if( !IsPostBack){
//your current code
}
}
and it should work.

Resources