English 中文(简体)
带有 sql 语句的 Databind 网格视图编辑项目板下载列表
原标题:databind gridview edititemtemplate dropdownlist with sql statement

我有以下代码的网格视图:

<asp:TemplateField HeaderText="Column1">
    <EditItemTemplate>
        <asp:Label ID="Label1" runat="server" Text= <%# Bind("Column1") %> ></asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="125px">
    </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

我要绑定一个 Sql 语句以填充下调列表...

select Column1 from Table1

我会通过后面的代码做吗? 感谢任何信息? 另外,根据用户对下调列表的选择, 我想用相应的数据填充下一栏(栏2)... 也需要帮助...

我对模板字段不熟悉, 我可以用一个电网视图, 装订在背后和通过 html 的代码, 但是模板字段就像另一种语言... 我很感激你的帮助!

问题回答

回答你的问题有几个部分, 我正在做一些假设(你用视觉工作室作为IDE, 你可以用VB作为代码背后的语言):

我要绑定一个 Sql 语句以填充下调列表...

您可以在代码内或通过视觉工作室 GUI 中做到这一点。 您可以使用一个下调列表的边框, 模板字段最终会给予您更大的灵活性 。 我将在模板字段上读取( http:// msdn. microsoft. com/ en- us/library/ bb288032. aspx” rel = “ nofollow” > here ), 因为除了基本数据显示之外, 它将是您使用 Gridview 的朋友 。 使用 GUI, 选择下调列表应该给您上右侧的小箭头, 这样您就可以创建数据连接和数据源, 以便与您的下调列表绑在一起 。

Also based on the selection the user makes with this dropdownlist, I want to populate the next column (column2) with the corresponding data

这要复杂一些, 因为您需要触发下调列表中的 PostBack (使用 AutoPostBack), 处理下调列表 s 选中的 Index changed 事件, 发现第二个栏中的控件需要更新, 并根据您下调列表中选中的索引/ 项目/ 值更新此控件。 有几种方法可以做到这一点, 但这里是我找到的最快的( 使用 asp. net winforms ) 。 我正在使用 SQL 冒险工作数据库, 在第一栏中的模板字段中与 雇员ID 嵌入一个下调, 并使用所选的雇员ID 在第二栏中填充一个标签, 与该雇员ID 管理器ID 。

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    EnableModelValidation="True" AutoGenerateColumns="False" 
    DataKeyNames="EmployeeID">
    <Columns>
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
            InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
        <asp:TemplateField>
            <ItemTemplate>
                Select EmployeeID
                <asp:DropDownList ID="ddEmpID" runat="server" OnSelectedIndexChanged="ddEmpID_SelectedIndexChanged" 
                    DataSourceID="SqlDataSource1" DataTextField="EmployeeID" 
                    DataValueField="EmployeeID" AutoPostBack="True">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="labManagerID" runat="server" Text="ManagerID"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Data Source=MyServerInstance;Initial Catalog=AdventureWorks;Integrated Security=True" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT TOP(10) EmployeeID, ManagerID FROM HumanResources.Employee">
</asp:SqlDataSource>

和从代码后方选择的 Index 更改的事件 :

Protected Sub ddEmpID_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim myDDList As DropDownList = CType(sender, DropDownList)
    Dim gvRow As GridViewRow = CType(myDDList.NamingContainer, GridViewRow)
    Dim myLabel As Label = CType(gvRow.FindControl("labManagerID"), Label)

      Create your sql query here to populate a data object and assign values throughout your row
        Dim myConnection As SqlConnection = New SqlConnection("Data Source=MyServerInstance;Initial Catalog=AdventureWorks;Integrated Security=True")
        Dim myCmd As SqlCommand = New SqlCommand("Select ManagerID FROM HumanResources.Employee WHERE EmployeeID= " + myDDList.SelectedValue + " ", myConnection)

        If myConnection.State = ConnectionState.Closed Then myConnection.Open()
        myLabel.Text = myCmd.ExecuteScalar
        If myConnection.State = ConnectionState.Open Then myConnection.Close()

End Sub

因为与Gridview合作有时是一种自焚练习, 我建议拥有一些好的 漫步辅导 手边:

- J,J,J,J,J,J,J,J,J,J,J,J,J,J,J,J





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签