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"));
}
Related
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"));
}
}
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 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();
}
}
This is a Master-Detail form. Master is a GridView. And, the Detail is a DetailsView.
The entire thing is achieved programmatically.
As you can see from the code, DetailsView is using the Master-objects's ID to retrieve the Detail items.
I need to make the ID column of the Master-GridView invisible. Coz, it is irrelevent for the user of the page. But it must not harm the page logic.
But the code-line, GridView1.Columns[1].Visible = false; is generating an exception.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How should I solve this problem?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
List<Order> orders = Order.Get();
GridView1.DataSource = orders;
GridView1.DataBind();
// This is giving Error...............!!!
GridView1.Columns[1].Visible = false;
// At first, when the page first loads,
// GridView1.SelectedIndex == -1
// So, this is done to automatically select the 1st item.
if (GridView1.SelectedIndex < 0)
{
GridView1.SelectedIndex = 0;
}
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView1.Rows[selRowIndex].Cells[1].Text);
Order master = Order.Get(selMasterId);
labItemsCount.Text = master.Items.Count.ToString();
DetailsView1.DataSource = master.Items;
DetailsView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
BindData();
}
}
Have you considered using the DataKeyNames property of the gridview? This way you can remove the 'id' column from the GridView bu still access the 'id' value in the Page_Load.
DataKeyNames = "id"
Then you can get the value of the id like this.
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView.DataKeys[selRowIndex].Value);
Order master = Order.Get(selMasterId);
Alternately, you could try changing the visibility of the column in the OnRowBound event of the GridView.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}