data binding to dropdownlist in asp.net? - asp.net

For a Label in we bind a data using
<asp:Label ID="Label2" runat="server" Text='<%#Eval("address") %>'></asp:Label>
how to bind data to dropdownlist like that?
asp:DropDownList ID="droplist" runat="server" >
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>

Like this....
<asp:DropDownList ID="droplist" runat="server" SelectedValue='<%#Eval("fieldname")%>'>
<asp:ListItem Text="admin"></asp:ListItem>
<asp:ListItem Text="manager"></asp:ListItem>
</asp:DropDownList>
Note that intellisense will not pick SelectedValue out. You will of course need to populate the dropdown with the data... using any method that suits

Either put your datasource in the DropDownList declaration like here: populate dropdownlist
Or use Codebehind like this:
Eg: inside Page_Load():
List<string> ItemsToGoInDropDown = new List<string>{"manager", "admin", "etc"};
droplist.DataSource = ItemsToGoInDropDown;
droplist.DataBind();

Put the data in hidden field. Then asigen that in drop down in Gridview Rowdatabound Event.Like This.
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hf = (HiddenField)e.Row.FindControl("hf");
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
ddl.SelectedValue = hf.Value;
}

Related

how to bind a value of datasource to a checkbox in datalist with Eval()

I have a datalist in my asp.net page. I bind a datasource to it in codebehind
and I have a checkbox in this datalist.
var n = from gi in DataContext.Context.GalleryImages
join g in DataContext.Context.Galleries
on gi.GalleryID equals g.GalleryID
where g.UserID == UserID && gi.GalleryID==GalleryID
select new
{
GalleryID = g.GalleryID,
ImageDescription = gi.ImageDescription,
GalleryName = g.GalleryName,
ImageFileName = gi.ImageFileName,
IsAlbumImage = gi.IsAlbumImage,
ImageID=gi.ImageID
};
dlGalleryList.DataSource = n;
dlGalleryList.DataBind();
When the "IsAlbumImage " is true the checkbox should be checked.
How can I bind this property to the checkbox?
It should be bind like:
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
Actually you have to ways to bind checkbox in a datalist
1- (recommended) Binding it directly from the ASP code using the Bind or Eval
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
2- Binding it on the ItemDataBound Event
First you will add the event handler to your datalist control, and adds the Boolean value to a datakey to be used in itemdatabound event
<asp:DataList ID = "DataList1" OnItemDataBound="DataListItemEventHandler" DataKeys = "IsAlbumImage"/>
Then you add the C# code that bind this
protected void DataListItemEventHandler(object sender, DataListItemEventArgs e)
{
CheckBox checkbx = new CheckBox();
checkbx = (CheckBox)e.Item.FindControl("MyCheckBox");
checkbx.Checked = (bool) DataList1.DataKeys(e.Item.ItemIndex)("IsAlbumImage");
}
Like this:
<asp:CheckBox
ID="check"
runat="server"
Checked='<%# Eval("column_name").ToString().Equals("1") %>'
/>

Event not firing as expected for control in formview asp.net

I have a LinkButton in an InsertItemTemplate which when clicked, should display a hidden DropDownList in the InsertItemTemplate. However, it doesn't seem to be working, but it will say, change the text of a label outside the Formview when the LinkButton is clicked. The event is firing, but the part to make the DropDownList visible in the InsertItemTemplate is not doing anything. Code is below:
.aspx:
<asp:FormView ID="formViewNewRecord" runat="server">
<InsertItemTemplate>
<asp:DropDownList ID="ddlAddSelection2" runat="server" DataSourceID="dSource1" DataTextField="Users" DataValueField="Users" AppendDataBoundItems="true" Visible="false">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton runat="server" ID="lbAddAnother" OnClick="lbAddAnother_Click">+Add Another</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
C#:
protected void lbAddAnother_Click(object sender, EventArgs e)
{
DropDownList addSelection2 = (DropDownList)formViewNewItem.Row.Cells[0].FindControl("ddlAddSelection2");
addSelection2.Visible = true;
Label2.Text = addSelection2.ID;
}
Your dropdown control is not an immediate child of your formview. So since the FindControl call is not recursive, you have to search for the control in the right location of your form view's child controls. See this for the details but at a high level, you need something along the lines of:
DropDownList ctrl = (DropDownList)FormView1.Row.Cells[0].FindControl("ddlAddSelection2");
After that, you should check it for null for safe measure.

dropdown in gridview, find GridRow on OnSelectedIndexChanged

I have a asp.net gridview. A template field of type dropdownlist is contained in a column:
<asp:TemplateField HeaderTextLabel="strManagedOETeamart">
<ItemTemplate>
<asp:DropDownList runat="server" AutoPostBack="True" OnSelectedIndexChanged="SelectedTeamartChanged">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
How can I get the GridViewRow when the user selects another item?
protected void SelectedTeamartChanged(object sender, EventArgs e)
{
DropDownList dropDown = (DropDownList) sender;
//I would like to know the GridViewRow this DropDownList is in
}
You're looking for the NamingContainer property:
GridViewItem row = (GridViewRow)dropDown.NamingContainer;
By the way, that works for any kind of web databound control like GridView, DataList,Repeater or ListView.
Consider this more complex requirement: you have a GridView which is nested in another GridView. Now you're handling a DropDownList's SelectedIndexChanged event that is inside the child's GridView and you want to get the reference to the GridViewRow of the parent GridView:
var control = (Control)sender;
var row = (GridViewRow)control.NamingContainer;
var parentRow = (GridViewRow)row.NamingContainer.NamingContainer;
That's the safest and easiest way to get it.

Dropdownlist objectdatasource

I have a dropdownlist connected to a objectdatasource. How can I get the DataValueField=TypeId ? I use the onselectedindexchanged to get the selected Type, how can I do the same with TypeId?
<asp:DropDownList ID="DropDownList" runat="server"
DataSourceID="ObjectDataSource" DataTextField="Type"
DataValueField="TypeId" AutoPostBack="true"
onselectedindexchanged="DropDownList_SelectedIndexChanged">
</asp:DropDownList>
To get the value:
int id = int.Parse(DropDownList.SelectedValue);
Not sure if I understand....do you want to get the selected value? In this case, this is what you have to do
string typeId = DropDownList.SelectedValue

Use data in repeater when Checkbox is check in ASP.net

I have a repeater for showing my data . this repeater showing 2 field that one of feild is checkBox Control and other is a lable.
NOW , how can I understand text of lable when the checkBox is Checked?
I want to see text of lable in evry row that the CheckBoxes is checksd.
how do I do?
I use LINQtoSQL for get and set data from database
On postback, you need to loop through every row of your repeater, and grab out the checkbox control. Then you can access it's .Checked and .Text properties. If it's .Checked, then add it to a list or array. I can elaborate if needed..
Page...
<asp:CheckBox ID="chkBoxID" runat="server" OnCommand="doSomething_Checked" CommandArgument="<%# Some Binding Information%>"
CommandName="NameForArgument">
</asp:CheckBox>
Code Behind...
protected void doSomething_Checked(object sender, CommandEventArgs e) {
CheckBox ctrl = (CheckBox)sender;
RepeaterItem rpItem = ctrl.NamingContainer as RepeaterItem;
if (rpItem != null) {
CheckBox chkBox = (LinkButton)rpItem.FindControl("chkBoxID");
chkBox.DoSomethingHere...
}
}
<asp:Repeater ID="rptX" runat="server">
<ItemTemplate>
<asp:Label ID="lblX" runat="server" Visible='<%# Eval("IsChecked") %>' />
<asp:CheckBox ID="chkX" runat="server" Checked='<%# Eval("IsChecked") %>' />
</ItemTemplate>
</asp:Repeater>
And code behind when you assign your data
rptX.DataSource = SomeIEnumerableFromLinq; // which has a bool field called IsChecked
rptX.DataBind();

Resources