Modify an asp:BulletedList in C# - c#

I've different buttons like this
<asp:Button ID="button1" runat="server" Text="single" title="single" EnableViewState="false" />
When this button is clicked I want that the text in a list like this changes
<asp:BulletedList ID="list1" runat="server" EnableViewState="false">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
<asp:ListItem>Item3</asp:ListItem>
<asp:ListItem>Item4</asp:ListItem>
</asp:BulletedList>
How in C# can I select the first, the second or the third element of the list?
protected button_Click(object sender, EventArgs e)
{
list1. ...;
}
Thanks.

Use ListControl.Items Property.
protected button_Click(object sender, EventArgs e)
{
list1.Items[0] ...;
}

For example if you want to change you Item1 to dir, use this :
list1.Items[0].Attributes["class"] = "dir";

Related

Checking if the value of a textbox is changed or not

I have a textbox filled at the page load. I want to check the value of the textbox is changed or not in the "update" button press. Any solution? TIA.
Well, you could say use client side javascript.
But, you could also do this:
Say this text box:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Done - continue" OnClick="Button1_Click" />
And our code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to setup and load controls
TextBox1.Text = "Dog";
ViewState["TextBox1"] = "Dog";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != (string)ViewState["TextBox1"])
{
// then text box was changed
}
}

persist ddl values and selected value in between postbacks

I have a save button and a drop down list on a page. Inside the page Load, the drop down list is populated if !Page.PostBack (AutoPostBack=false). So, the first time I load the page, the drop down list is populated. I also have a save method to go with the save button. When this button is clicked, it should do something with the selected value of the drop down list. My problem is that the drop down list has no value (is null) inside the button save method. How would you fix this?
Markup:
MyClass.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MyClass" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainRegion" runat="server">
<div>
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="myDdlChange" ViewStateMode="Enabled" EnableViewState="true" />
</div>
<br />
<div style="min-width: 300px; max-width: 770px;">
<asp:TextBox id="txtBox" runat="server" TextMode="MultiLine" />
</div>
<div class="buttonContainer">
<span >
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</span>
</div>
</asp:Content>
Then, in the code behind:
MyClass.aspx.cs
public class MyClass
{
protected global::System.Web.UI.WebControls.DropDownList myDdl;
protected global::System.Web.UI.WebControls.TextBox txtBox;
protected global::System.Web.UI.WebControls.Button btnSave;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page.IsPostBack)
Session["selectedID"] = myDdl.SelectedValue; // my attempt to put the selected value from ddl in a session var, to use it later inside the save method but it didn't work
if (!Page.IsPostBack)
{
//create array1 here
myDdl.Items.Clear();
myDdl.Items.AddRange(array1);
Session["selectedID"] = myDdl.SelectedValue;
myDdlChange(null, null);
this.DataBind();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
//do something based on myDdl.SelectedValue (which shouldn't be null)
}
protected void myDdlChange(object source, EventArgs e)
{
txtBox.Text = myDdl.SelectedValue;
}
}
}
I think the main problem is you should be using Page_Load instead of OnLoad.
You don't need to use Session to remember the SelectedValue.
Try something like this which works for me...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateDropdown();
}
private void PopulateDropdown()
{
myDdl.Items.Clear();
var array1 = new ListItem[3];
array1[0] = new ListItem("item1", "item1");
array1[1] = new ListItem("item2", "item2");
array1[2] = new ListItem("item3", "item3");
myDdl.Items.AddRange(array1);
myDdl.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
var selectedVal = myDdl.SelectedValue; // putting a breakpoint here shows myDdl.SelectedValue is not null
}
protected void myDdlChange(object sender, EventArgs e)
{
}

Input text box value is not changing

In my .aspx page I have a 'input' html tag, also an asp button.
<input id="Name" type="text" runat="server" clientidmode="Static" />
<asp:Button Width="100" type="submit" ID="sendOrder" runat="server" OnClick="SubmitForm" Text="Submit" />
On page load, I am filling the value in input tag from code behind, like this:
Name.Value= "X";
But now if I change value of this text box from browser, lets say "Y", and click on Submit button, then I get the old value, but not the new one.
protected void SubmitForm(object sender, EventArgs e)
{
var test= Name.Value; // here I get old value
}
How can I get the altered value?
Make sure you are only setting the value to "X" when its not a postback:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load() event will change the value from "Y" back to "X".
You need to use !IsPostBack on Page_Load shown as below:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input> control in asp.net but best practice is to use <asp:TextBox></asp:TextBox> control instead.
Here is the sample example:
HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}
Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!
You don't need all the else portion, just do this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}

How to manage a Two-Way databinding in an ASP.NET web control?

I have a question about how to perform a two-way databinding in an ASP.NET control (in this example a repeater control). The technology is Web Forms. My question is, where can I read the user's data after a postback? (See MyButton_Click below).
Markup:
<asp:Repeater runat="server" ID="MyRepeater">
<ItemTemplate>
<asp:Label runat="server">Name: </asp:Label><asp:TextBox ID="MyTextBox" runat="server" Text='<%#Bind("Name") %>'></asp:TextBox><br/>
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" ID="MyButton" Text="Hit me" OnClick="MyButton_Click" />
Interface:
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
MyRepeater.DataSource = (
from d in Data.GetData()
select new
{
Name = d
}).ToList();
MyRepeater.DataBind();
}
protected void MyButton_Click(object sender, EventArgs e)
{
// Ok now, where is the user's changed data...?
}
Ok, I've found that it is not possible to do two way binding in this way. I though of two way binding as a way to have the assigned data source to be updated automatically, but that seems only possible when using the DataSource web controls, such as SqlDataSource, XmlDataSource, etc. But then it seems not possible to customize the processing of the data.
A solution is to loop through the repeater items and simply read the control explicitly. So the answer to the question and how to get the user's data is:
protected void MyButton_Click(object sender, EventArgs e)
foreach (RepeaterItem repeaterItem in MyRepeater.Items) {
TextBox myTextBox = (TextBox)repeaterItem.FindControl("MyTextBox");
var usersData = myTextBox.Text; // Here it is!
}
}

SelectedIndexChanged doesn't work!

My code:
*.aspx:
<asp:DropDownList ID="CountryList" CssClass="CountryList" runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged" />
*.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
CountryList.SelectedIndexChanged +=
new EventHandler(CountryList_SelectedIndexChanged);
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}
But this doesn't work.
Try setting AutoPostBack="true" on this dropdown list:
<asp:DropDownList
ID="CountryList"
CssClass="CountryList"
runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged"
AutoPostBack="true"
/>
Also you don't need to manually wire-up the event handler in the Page_Load method. It will be automatically done by ASP.NET when it compiles the webform:
protected void Page_Load(object sender, EventArgs e)
{
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}
I think you missed AutoPostBack="true" Property in aspx file
Add AutoPostBack="true" in ur aspx code and everything will work as you thought.

Categories