So I have a sipme ASP.NET Web Page that prints the info of one's gender stacked dynamically in a radio button list:
protected void Page_Load(object sender, EventArgs e)
{
String[] genders = new String[2];
genders[0] = "Male";
genders[1] = "Female";
RadioButtonList1.DataSource = genders;
RadioButtonList1.DataBind();
RadioButtonList1.Items.Add(new ListItem("Neutral", "Zero"));
}
protected void Button1_Click(object sender, EventArgs e)
{
lblName.Text = txtName.Text;
lblSurname.Text = txtSurname.Text;
lblEmail.Text = txtMail.Text;
Panel1.Visible = true;
if (RadioButtonList1.SelectedIndex==0) lblGender.Text = "Male";
else lblGender.Text = "Female";
}
However when I launch the site no matter what I select it always writes female it's like the RadioButtonList1.SelectedIndex==0 isn't working.
Any ideas?
Bind radiobuttonlist in page_Load with !IsPostBack condition or your radiobuttonlist will bind every time you post your page. So your radiobuttonlist is reset to index -1 when you click the button_click.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
String[] genders = new String[2];
genders[0] = "Male";
genders[1] = "Female";
RadioButtonList1.DataSource = genders;
RadioButtonList1.DataBind();
RadioButtonList1.Items.Add(new ListItem("Neutral", "Zero"));
}
}
Related
Is this possible?
I have a table with user accounts retrieved from a database. Then at the start of each column I would like to add a checkbox which if selected will select the account. I tried experimenting with it and I can't seem to put the checkbox inside the table. How can I do this?
You can try this:
const int ColumnSelect = 0;
protected void Page_Load(object sender, EventArgs e)
{
//Get real data here.
DataTable dt = new DataTable();
dt.Columns.Add("count");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "5";
GridView1.Columns.Add(new TemplateField());
BoundField b = new BoundField();
GridView1.Columns.Add(b);
b.DataField = "count";
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
//Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
if (cb.Checked)
{
//Do something here.
}
}
}
I want to findcontrol on griview after DDL OnSelectedIndexChanged event. where the target control is on the rowindex where the DDL is located..
here my codes;
protected void Page_Load(object sender, EventArgs e)
{
ArrayList Dummysource = new ArrayList() { "AA", "BB", "CC", "DD" };
if(!IsPostBack )
{
GridView1.DataSource = Dummysource;
GridView1.DataBind();
}
}
protected void ddlsample_OnSelectedIndexChanged(object sender, EventArgs e)
{
string valueComponent = (sender as DropDownList).SelectedItem.Value;
Label1.Text = valueComponent;
}
int ddlvalue;
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking whether the Row is Data Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Finding the Dropdown control.
DropDownList ddlsample = (DropDownList)e.Row.FindControl("ddlsample");
Label ilbldata = (Label)e.Row.FindControl("lbldata");
if (ddlsample != null)
{
switch(ilbldata.Text)
{
case "AA":
ddlvalue = 2;
break;
case "BB":
ddlvalue = 3;
break;
case "CC":
ddlvalue = 4;
break;
case "DD":
ddlvalue = 5;
break;
}
for (int i = 1; i <= ddlvalue; i++ )
{
ddlsample.Items.Add(i.ToString() );
}
}
}
}
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridView gv = sender as GridView;
gv = GridView1;
Label foo = gv.SelectedRow.FindControl("lbldata") as Label ;
Label2.Text = foo.Text;
}
the code get the value of the DropDownList selected Item. I'm Wondering on how to get the component value in the gridview. after selectedindexchange event of DDL
I made some visual photo for more clear
http://i1288.photobucket.com/albums/b493/Kasparov1/GridviewDDL_zps3721fb97.png
thanks in advance;
Try this
protected void ddlsample_OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Label1.Text = ddl.SelectedItem.Value;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
// Find your control
Control control = row.FindControl("myControl");
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
DropDownList drop = GridView1.Controls[0].Controls[0].FindControl("DropDownList1") as DropDownList;
string text = drop.Items[drop.SelectedIndex].ToString();
//Find FooterRow Control
DropDownList dT = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
string text = dT.Items[dT.SelectedIndex].ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//DropDownList1 in GridVied
{
//Find FooterRow Control
DropDownList drop = GridView1.FooterRow.FindControl("DropDownList1") as DropDownList;
string text = drop.Items[drop.SelectedIndex].ToString();
//find normal DropDownList1
DropDownList drop1 = GridView1.FindControl("DropDownList1") as DropDownList;
string text = drop1.Items[drop1.SelectedIndex].ToString();
}
//ADD list in GRIDVIEW dropdownlist at run time
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");//Gridview DropDownList
ddl.Items.Add("- - Select - -");
ddl.Items.Add(new ListItem("ABCD"));
ddl.Items.Add(new ListItem("EFGH"));
}
I have an edit page where I set the selected index of a radcombobox (rcb_ParentCompany) based on a value returned from the database. However on postback the text in the combobox keeps changing to the top item in the dataset. Any ideas why?
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
BindOperatingNameComboBox(rcb_OperatingName);
BindParentCompanyComboBox(rcb_ParentCompany);
}
}
protected void btn_Edit_Command(object sender, CommandEventArgs e)
{
Client ClientToEdit = ClientController.ViewClient(int.Parse(e.CommandArgument.ToString()));
//Populate Client fields
txt_ClientName.Text = ClientToEdit.ClientName;
rcb_OperatingName.Text = ClientToEdit.OperatingName;
int ParentCompanyIndex = rcb_ParentCompany.FindItemIndexByValue(ClientToEdit.ParentCompanyID.ToString());
rcb_ParentCompany.SelectedIndex = ParentCompanyIndex;
txt_Address1.Text = ClientToEdit.Address1;
txt_Address2.Text = ClientToEdit.Address2;
txt_Country.Text = ClientToEdit.Country;
txt_Region.Text = ClientToEdit.Region;
txt_City.Text = ClientToEdit.City;
txt_PostalCode.Text = ClientToEdit.PostalCode;
txt_ClientNote.Text = ClientToEdit.ClientNote;
tbl_EditServices.Controls.Clear();
PopulateEditClientPanel(ClientToEdit);
btn_SaveChanges.CommandArgument = e.CommandArgument.ToString();
btn_Cancel.CommandArgument = e.CommandArgument.ToString();
}
protected void BindParentCompanyComboBox(RadComboBox ComboBox)
{
DataTable OperatingNames = ClientController.GetExistingClientAndOperatingNames("");
ComboBox.DataTextField = "ClientName";
ComboBox.DataValueField = "ClientID";
ComboBox.DataSource = OperatingNames;
ComboBox.DataBind();
}
protected void rcb_ParentCompany_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
BindParentCompanyComboBox((sender as RadComboBox));
}
Any ideas why?
Yes, because you are doing if(IsPostBack) as opposed to if(!IsPostBack)
I have one TextBox inside a UserControl, and this UserControl is repeating inside Repeater.
But, when user fills TextBox with values and after that I can't get values from TextBoxs.
default.aspx:
protected void Page_Load(object sender, EventArgs e)
{
//filling repeater with dataset
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
On button1 click I'm trying to fill List<string> with values from textbox.texts
protected void Button1_Click(object sender, EventArgs e)
{
List<string> sss = new List<string>();
foreach (Control i in Repeater1.Controls)
{
foreach (Control item in i.Controls)
{
if (item is WebUserControl1)
sss.Add(((WebUserControl1)item).getString);
}
}
}
And UserControl code:
public string getString
{
get
{ return TextBox1.Text; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
you should loop on all repeater's items and use FindControl to find your user control then call the getString method on such found instances, pseudo-code (not tested):
foreach(var rptItem in Repeater1.Items)
{
WebUserControl1 itemUserControl = ((WebUserControl1)rptItem .FindControl("WebUserControl1"))
if(itemUserControl != null)
{
var itemText = itemUserControl.getString();
}
}
I want to preserve the dynamically created control when postback occurs .
protected void Page_Load(object sender, EventArgs e)
{
}
private void CreateTable()
{
HtmlTableRow objHtmlTblRow = new HtmlTableRow();
HtmlTableCell objHtmlTableCell = new HtmlTableCell();
objHtmlTableCell.Controls.Add(new TextBox());
objHtmlTblRow.Cells.Add(objHtmlTableCell);
mytable.Rows.Add(objHtmlTblRow);
this.SaveControlState();
// this.Controls.Add(mytable);
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateTable();
}
It can be achieved by calling CreateTable() in Page_Load. Is there any alternative way to preserve the control
Thanks
You can add them to a List when you create them and save your List to Session. On postback (Page_Load) load them from your Session to your Page.
the below code should work
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateTable()
}
public static Control GetPostBackControl(Page thisPage)
{
Control ctrlPostedback = null;
string ctrlName = thisPage.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
{
ctrlPostedback = thisPage.FindControl(ctrlName);
}
else
{
foreach (string Item in thisPage.Request.Form)
{
Control c = thisPage.FindControl(Item);
if (((c) is System.Web.UI.WebControls.Button))
{
ctrlPostedback = c;
}
}
}
return ctrlPostedback;
}
The code works from UpdatePanel
Reference:http://www.asp.net/ajax/videos/how-to-dynamically-add-controls-to-a-web-page