Within my EditTemplate I have a DropDownList as such:
<EditItemTemplate>
<asp:DropDownList ID="ddlFixture" runat="server" AutoPostBack="True" onselectedindexchanged="fixtureSelected"
DataSourceID="FixtureDataSource" DataTextField="WTag" DataValueField="WTag" AppendDataBoundItems="true" SelectedValue='<%#Bind("Fixure") %>'>
</asp:DropDownList>
</EditItemTemplate>
When the user selects an item from the DropDownList, I like to populate other fields in that are in EditMode as well.
You will notice that on onselectedindexchanged="fixtureSelected" I am calling fixtureSelected. I have noted some of the issues I am facing:
protected void fixtureSelected(object sender, EventArgs e)
{
// Below I am trying to get value of ddlFixture but it cannot recognize ddlFixture. GettingThe name 'ddlFixture' does not exist in the current context
string fixtureId = ddlFixture.SelectedItem.Value;
// I also need to update the text in EditMode but this will not work either. Get similar message as for ddlFixture
txtCampus.Text = "Campus1";
}
Your problem is due to the fact that the controls your are trying to access are inside the edititemtemplate of your GridView.
To get to the dropdownlist instance, you can use:
DropDownList ddlFixture = sender as DropDownList;
And to get the other controls in edititemtemplate, you use:
ddlFixture.NamingContainer.FindControl("control_id")
For example:
TextBox txtCampus = ddlFixture.NamingContainer.FindControl("txtCampus") as TextBox;
Related
I need to get the value of the selected item in a dropdown list that's inside the edit template of a formview control. The formview ID is "fvDocRvwrs".
Here's the markup for the dropdownlist:
<asp:DropDownList SelectedValue='<%# Bind("rvwStat") %>' runat="server" ID="rvwStatDdl" CssClass="form-control" DataSourceID="sdsStatuses" DataTextField="stat" DataValueField="statIdPk" AppendDataBoundItems="true" OnSelectedIndexChanged="rvwStatDdl_SelectedIndexChanged"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList>
I'm just having some difficulty getting the SelectedValue using the onselectedindexchanged event of the dropdownlist. I am able to find the control using:
protected void rvwStatDdl_SelectedIndexChanged(object sender, EventArgs e)
{
var statVal = fvDocRvwrs.FindControl("rwStatDdl").ToString();
}
I just need to know how to populate a variable with the selected value.
I think you should cast as DropDownList after finding control
var statVal = ((DropDownList)fvDocRvwrs.FindControl("rwStatDdl")).SelectedValue.ToString();
You can access the SelectedValue property.
Try
protected void name_SelectedIndexChanged(object sender, EventArgs e){
DropDownList list = (DropDownList)sender;
string value = list.SelectedValue;}
Credit: Dropdownlist selected value at Selectedindexchanged event Frank Lee's answer
I need to read in the code behind the selected text (not the value) in the dataTextField of a dropdownlist that is nested in a FormView.
Here is my DDL:
<asp:DropDownList ID="DDL1" runat="server" DataSourceID="SQL1" dataTextField="name" DataValueField="IDname" CausesValidation="True" ClientIDMode="Static">
</asp:DropDownList>
And here is my code behind:
protected void UpdateButton_Click(object sender, EventArgs e)
{
DropDownList DDL1 = FV1.FindControl("DDL1") as DropDownList;
SQL3.UpdateParameters["ddlparam"].DefaultValue = DDL1.SelectedValue;
// Possible to get the text corresponding to the selectedValue?
}
So far so good. Now I want to get the text in the dataTextField corresponding to the selected value. Possible? and How?
Grab the text of the selected item with:
DDL1.SelectedItem.Text
you're looking for DDL.SelectedItem.Text MSDN
I am using 3 DropDownList inside DataList. So, each row contains 3 DropDownList. DataList also contains DataKeyField.
Now, if user select value from DropDownList1 then I want to bind DropDownList2 and if user select value from DropDownList2 then I want to bind DropDownList3. I am using SelectedIndexChanged and I am able to get value of related DropDownList selected value. But, If user select DropDownList2 then I also need value of DropDownList1 and also need value of respected DataKeyField.
How to get this ???
My code Sample:
<asp:DataList ID="dlTest" runat="server" OnItemDataBound="dlTest_ItemDataBound"
DataKeyField="TestId">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged"> </asp:DropDownList>
</ItemTemplate>
</asp:DataList>
Here, onSelectedIndexChanged of DropDownList3, I am able to get selectedValue of it but I also need respected row's selectedValue of DropDownList1 and DropDownList2 and also respected DataKeyField value.
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList3 = (DropDownList)sender;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
// Also need selectedValue of DropDownList1 and DropDownList2 and DataKeyField.
}
I think you are almost there. You just need to. Using your code:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList1 = (DropDownList)sender;
var DropDownList2 = (DropDownList)sender;
var DropDownList3 = (DropDownList)sender;
string DDL1 = ((DropDownList)DropDownList1.NamingContainer.FindControl("DropDownList1")).SelectedValue;
string DDL2 = ((DropDownList)DropDownList2.NamingContainer.FindControl("DropDownList2")).SelectedValue;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
}
Either way I would do it a little bit different:
item valueddl1 = DropDownList1.SelectedValue;
And so on; which should work as weel and it is a little bit more simple.
I have created a DataList in asp.net -
<asp:DataList runat="server" ID="pTextBox">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPN" runat="server" Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
This creates checkBoxes and textBoxes based on the string values passed through from a webService.
How can I get the profileTextBox Text string value when a user clicks CheckBoxPN and populate another textBox outwith the DataList on the page with the string value??
You can use the CheckedChanged event of the CheckBox and cast it's NamingContainer to DataListItem, the you just have to use FindControl to find a different server control:
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox) sender;
DataListItem item = (DataListItem) chk.NamingContainer;
TextBox txt = (TextBox) item.FindControl("profileTextBox");
this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}
By the way, this approach works with any web-databound control(Repeater, GridView, etc.)
I have a dropdownlist inside of a TemplateField, within a GridView.
I would like to dynamically add list items to it and write code to handle when the index changes. How do I go about manipulating the list, since I can't directly reference the DropDownList when it's in a TemplateField.
Here is my code:
<asp:TemplateField HeaderText="Transfer Location" Visible="false">
<EditItemTemplate>
<asp:DropDownList ID="ddlTransferLocation" runat="server" ></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
If I'm understanding what you want to do correctly, you can handle adding items to your drop down like this:
foreach (GridViewRow currentRow in gvMyGrid.Rows)
{
DropDownList myDropDown = (currentRow.FindControl("ddlTransferLocation") as DropDownList);
if (myDropDown != null)
{
myDropDown.Items.Add(new ListItem("some text", "a value"));
}
}
Then, if you mean handling the index change of the DropDownList you just need to add an event handler to your control:
<asp:DropDownList ID="ddlTransferLocation" runat="server" OnSelectedIndexChanged="ddlTransferLocation_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
Then in that event handler you can use (sender as DropDownList) to get whatever you need from it:
protected void ddlTransferLocation_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList myDropDown = (sender as DropDownList);
if (myDropDown != null) // do something
{
}
}