我想绑定下拉列表 [英] i want to bind dropdownlist
问题描述
helloo我实际上想将gridview中的dropdownlist与数据库绑定,我已经在此显示了这个gridview示例的完整代码,
helloo i want to bind dropdownlist in gridview with database actualy i have complete code of this gridview example which i show here,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:customerConnectionString %>"
SelectCommand="SELECT [id], [name], [gender], [city], [state] FROM [cust_info]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name"> <EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:DropDownList ID="cmbGender" runat="server" SelectedValue='<%# Eval("Gender") %>'>
<asp:ListItem Value="M" Text="Male"></asp:ListItem>
<asp:ListItem Value="F" Text="Female"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbGender" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewGender" runat="server" >
<asp:ListItem Selected="True" Text="Male" Value="M"></asp:ListItem>
<asp:ListItem Text="Female" Value="F"></asp:ListItem> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewCity" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="State">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewState" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("state") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'>
<asp:ListItem Value="value" Selected="True"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
但主要问题出在本节中
but the main problem come out from this section
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'>
<asp:ListItem Value="value" Selected="True"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
如何将数据库中的dropdownlist绑定到我希望数据库中的值到下拉列表中,请向我建议此示例的正确编码
谢谢
how can i bind dropdownlist from database i want the value from database into dropdown list pls suggest me proper coding of this example
thank you
推荐答案
ConnectionStrings:customerConnectionString %> " span> =" 从[cust_info]中选择[id],[name],[gender],[city],[state]" < /asp:SqlDataSource > < asp:GridView ID =" runat 服务器" ShowFooter true" AutoGenerateColumns 错误" OnRowCommand =" > < 列 > < asp:TemplateField HeaderText =" SortExpression 名称" < > < asp:TextBox ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:TextBox > < /EditItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" < > < /FooterTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Bind(" )%> ' > < /asp:Label > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < EditItemTemplate > < asp:DropDownList ID =" runat 服务器" SelectedValue <%#Eval(" )> < asp:ListItem 值 =" 文本 男" < > < asp:ListItem 值 =" 文本 女性" < > < /asp:DropDownList > < /EditItemTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:Label > < /ItemTemplate > < FooterTemplate > < asp:DropDownList ID="cmbNewGender" runat="server " > <asp:ListItem Selected="True" Text="Male" Value="M"></asp:ListItem> <asp:ListItem Text="Female" Value="F"></asp:ListItem> </asp:DropDownList> < /FooterTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < EditItemTemplate > <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("city") %>'></asp:TextBox> < /EditItemTemplate > < FooterTemplate > <asp:TextBox ID="txtNewCity" runat="server" ></asp:TextBox> < /FooterTemplate > < ItemTemplate > <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label> < /ItemTemplate > < /asp:TemplateField > <asp:TemplateField HeaderText="State" SortExpression="State"> < EditItemTemplate > <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label> < /EditItemTemplate > < FooterTemplate > <asp:TextBox ID="txtNewState" runat="server" ></asp:TextBox> < /FooterTemplate > < ItemTemplate > <asp:Label ID="Label4" runat="server" Text='<%# Bind("state") %>'></asp:Label> < /ItemTemplate > < /asp:TemplateField > <asp:TemplateField HeaderText="Type"> < EditItemTemplate > < asp:DropDownList ID =" runat 服务器" DataTextField 类型" DataValueField 类型" SelectedValue =' <%# Eval(" ) %> ' > <asp:ListItem Value="value" Selected="True"></asp:ListItem> < /asp:DropDownList > < /EditItemTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:Label > < /ItemTemplate > < FooterTemplate > < asp:DropDownList ID =" runat 服务器" DataTextField 类型" DataValueField 类型" < > < /FooterTemplate > < /asp:TemplateField > <asp:TemplateField HeaderText="Edit" ShowHeader="False"> < EditItemTemplate > <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:Lin kButton > <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> < /EditItemTemplate > < FooterTemplate > <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> < /FooterTemplate > < ItemTemplate > <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkBu tton > < /ItemTemplate > < /asp:TemplateField > <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> < /列 > < /asp:GridView > < /div > < /form > < /body > < /html >
ConnectionStrings:customerConnectionString %>" SelectCommand="SELECT [id], [name], [gender], [city], [state] FROM [cust_info]"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="Name" SortExpression="Name"> <EditItemTemplate> <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Gender"> <EditItemTemplate> <asp:DropDownList ID="cmbGender" runat="server" SelectedValue='<%# Eval("Gender") %>'> <asp:ListItem Value="M" Text="Male"></asp:ListItem> <asp:ListItem Value="F" Text="Female"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lbGender" runat="server" Text='<%# Eval("gender") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="cmbNewGender" runat="server" > <asp:ListItem Selected="True" Text="Male" Value="M"></asp:ListItem> <asp:ListItem Text="Female" Value="F"></asp:ListItem> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <EditItemTemplate> <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("city") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewCity" runat="server" ></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="State" SortExpression="State"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewState" runat="server" ></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("state") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Type"> <EditItemTemplate> <asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'> <asp:ListItem Value="value" Selected="True"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Edit" ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </EditItemTemplate> <FooterTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> </FooterTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> </Columns> </asp:GridView> </div> </form> </body> </html>
but the main problem come out from this section
but the main problem come out from this section
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'>
<asp:ListItem Value="value" Selected="True"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
how can i bind dropdownlist from database i want the value from database into dropdown list pls suggest me proper coding of this example
thank you
how can i bind dropdownlist from database i want the value from database into dropdown list pls suggest me proper coding of this example
thank you
you can do this in different ways, one of them would be
Use rowdatabound event of the grid view. Find the dropdown control (e.Row.FindControl) and perform the databinding.
you can use your custom collection or a dataset as the data source, but make sure you have this populated before the rowdatabound, one of the place would be page load if its appropriate for you.
you can do this in different ways, one of them would be
Use rowdatabound event of the grid view. Find the dropdown control (e.Row.FindControl) and perform the databinding.
you can use your custom collection or a dataset as the data source, but make sure you have this populated before the rowdatabound, one of the place would be page load if its appropriate for you.
Hi there..
Define a sqldatasource in design view itself.. and add the value like this:
Hi there..
Define a sqldatasource in design view itself.. and add the value like this:
<asp:dropdownlist id="ddlBind" runar="server" datasource="SqlDataSource1" datatextfield="ColValue" datavaluefield="ColID" xmlns:asp="#unknown" />
这篇关于我想绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!