Dropdownlist selected value at Selectedindexchanged event - asp.net

I'm working on asp.net website with Vb.net and I have a dropdownlist with autopostback = true and I need to get the selected value when I change the item or I want to get the item which fires the selectedindexchanged event ..
any help please..

In ie. your Page_Load set
this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);
Then write the event handler like this:
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string selected = (string) comboBox.SelectedItem;
}
Make sure that in your Page_Load you write this before setting the combobox default value or you will end up with this always being the selected item:
if (Page.IsPostBack)
return;

try this:
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
string value = (string)list.SelectedValue;
}

If item is a Dictionary:
string value = ((KeyValuePair<string, string>)combobox.SelectedItem).Key;

Related

how to define radio button CheckedChanged property after finding radio button in another control

i like to know how i have to use radio button CheckedChanged property when the radio button there is in gridview and this gridview itself is inside of 1 user control and user control is inside of detail view control.
before i have learned how i have to find radio button control in another control. but after finding i do not know how to make CheckedChanged property for that?
protected void btnShowAddTransmittaltoCon_Click(object sender, EventArgs e)
{
Transmittallistfortest transmittalList = (Transmittallistfortest)DetailsView1.FindControl("Transmittallistfortest1");
GridView g3 = transmittalList.FindControl("GridViewTtransmittals") as GridView;
foreach (GridViewRow di in g3.Rows)
{
RadioButton rad = (RadioButton)di.FindControl("RadioButton1");
//Giving Error:Object reference not set to an instance of an object.
if (rad != null && rad.Checked)
{
var w = di.RowIndex;
Label1.Text = di.Cells[1].Text;
}
Replace this
RadioButton rad = (RadioButton)di.FindControl("RadioButton1");
with this:
RadioButton rad = di.FindControl("RadioButton1") as RadioButton;
You won't get an exception but it may return NULL - in which case it'll be caught in the ifstatement: rad != null.
The whole point of using the as keyword is this:
as => won't throw an exception - it just reports null.
By the way: You should retrieve the RadioButton this way:
if(di.RowType == DataControlRowType.DataRow)
{
RadioButton rad = di.FindControl("RadioButton1") as RadioButton;
}
To define the CheckedChange event, do this:
//rad.Checked = true;
rad.CheckedChanged += new EventHandler(MyCheckedChangeEventHandler);
Then define the handler:
protected void MyCheckedChangeEventHandler)(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
if (rb.Checked)
{
// Your logic here...
}
}

How to get datakeys value in a gridview

I try to delete a record in Gridview and Database .I added a boundfield button and set the CommandName to Deleterecord in the RowCommand event I want to get the primary key of this record to delete that(primary key value does not show in the grid view. The following block of code shows this event(I try to show some data in a text box):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
TextBox1.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
//bla bla
}
}
I also set
DataKeyName="sourceName"
in the gridview but it is not my primary key
If I click this button an exception occured like this:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How can I solve this problem
Use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
TextBox1.Text = grid.DataKeys[gvr.RowIndex].Value.ToString();
//bla bla
}
}
Here LinkButton should be replaced by the type of control which is performing this RowCommand Event
For more info go Here

How to find out rowindex when fire dropdown event inside gridview

I am using two dropdownlist in gridview. Total 5 to 7 rows in gridview.
Bind ProductId and product name in First Dropdown. when select product that time databound in second dropdown.
so how to findout row number when fire dropdown's event.
I assume the property you're looking for is GridViewRow.RowIndex. To get the reference to the row from the SelectedIndexChanged event from your DropDownLis(s), you can use it's NamingContainer property:
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int rowIndex = row.RowIndex;
// if you want to get the reference to the other DropDown in this row
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
}
You can handle SelectedIndexChanged event of DropDownList.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
string rowNumber= GridView1.DataKeys[gvr.RowIndex].Value.ToString();
}

Get the row that has been clicked in the gridview

I have added OnClick event dynamically for gridview.So when I click on any of the row on grid view, the event is fired but I can't get which row is clicked.
This is the code where I add event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnGrid, string.Empty);
}
//This is the code which catches this event
protected void GridView1_OnClick(object sender, EventArgs e)
{
_ID="10" //Static data. I need this from the gridview1.row[ClickedRow].cell[0]
}
I fear you'll have to "manually" assign this value.
For this, add hidden input element to the form:
<input type="hidden" name="clicked_row" />
Change the code to:
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnGrid, string.Empty) + "; document.forms[0].elements['clicked_row'].value = '" + e.Row.RowIndex + "';";
And finally, read the index from the Request in the GridView1_OnClick method:
_ID = Int32.Parse(Request.Form["clicked_row"]);
If each row has its own ID change e.Row.RowIndex to e.Row.Id.
I think, you could pass your id/rowindex as parameter to GetPostBackClientHyperlink() and implement the IPostBackEventHandler-interface in your page. The RaisePostBackEvent() method will get the id as input parameter passed in.
I haven't tested this, I just took a look at the example in MSDN documentation
Try passing in the row instead of the grid, like this:
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(e.Row, String.Empty);
And in your event handler:
protected void GridView1_OnClick(object sender, EventArgs e)
{
if (sender is GridViewRow)
{
int index = ((GridViewRow)sender).RowIndex;
}
}
I'm not positive, but I think the GridView OnClick will still fire since you're passing a child element of the grid.

How to programmatically set SelectedValue of Dropdownlist when it is bound to XmlDataSource

I'm using XmlDataSource as the datasource for a dropdownlist.
Now I want to set the SelectedValue of the drop down when the page initially loads. I have tried the OnDataBound event of the drop down in which I could see the total items. But setting the SelectedValue didn't work. InOnDataBinding event, I couldn't even see the total items probably because the list isn't bound yet?
How can I set the selected index based on a value?
This seems to work for me.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
}
}
DropDownList1.Items.FindByValue(stringValue).Selected = true;
should work.
This is working code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataTextField = "user_name";
DropDownList1.DataValueField = "user_id";
DropDownList1.DataSource = getData();// get the data into the list you can set it
DropDownList1.DataBind();
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("your default selected text"));
}
}
Have you tried, after calling DataBind on your DropDownList, to do something like ddl.SelectedIndex = 0 ?

Resources