下拉列表选定值绑定问题 [英] Dropdownlist Selected value Binding Problem

查看:57
本文介绍了下拉列表选定值绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="ObjectDataSource1"

            OnRowDataBound="RowDataBound" >
            <Columns>
                <asp:CommandField HeaderText="EDIT" ShowEditButton="True" />
                <asp:BoundField DataField="SNO" HeaderText="SNO" SortExpression="SNO" />
                <asp:TemplateField HeaderText="BASIC ICONURL" SortExpression="BASIC_ICONURL">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"

                            SelectedValue='<%# Bind("BASIC_ICONURL") %>' ></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("BASIC_ICONURL") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="BASIC_FILEURL" HeaderText="BASIC FILEURL"

                    SortExpression="BASIC_FILEURL" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"

                    ReadOnly="True" SortExpression="ID" Visible="False" />
            </Columns>
        </asp:GridView>











Imports System.Data
Imports MySql.Data
Imports MySql
Imports MySql.Data.MySqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim conn As MySqlConnection
    Dim cmd As MySqlCommand
    Dim connstr As String
    Dim strSQL As String
    Dim reader As MySqlDataReader

    Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then

            Dim GV As DropDownList = e.Row.FindControl("DropDownlist1")
            connstr = "server=localhost;User Id=xxxx;password=xxx;Persist Security Info=True;database=xxx"
            strSQL = "select * from pdfmap"
            conn = New MySqlConnection(connstr)
            cmd = New MySqlCommand(strSQL, conn)
            conn.Open()
            reader = cmd.ExecuteReader
            GV.DataSource = reader
            GV.DataTextField = "NAME"
            GV.DataValueField = "BASIC_ICONURL"
            GV.DataBind()
        End If

    End Sub
End Class




当我尝试以这种方式绑定时出现错误,当我删除"selectedvalue =<%#Bind("BASIC_ICONURL")%>时我没有收到错误消息,但数据没有更新.




When i am trying to Bind in this way im getting error, when i remove "selectedvalue=<%#Bind("BASIC_ICONURL")%>" im not getting error, but data is not updating.

推荐答案


对于您的情况,最简单的方法是为EditItemTemplate标记中的DropDownList使用SqlDataSource.您需要删除RowDataBound事件处理程序,并让此代码填充数据并将其绑定到您的DropDownList:

Hi,
The easiest way for your case is to use a SqlDataSource for your DropDownList in the EditItemTemplate tag. You need to remove RowDataBound event handler and let this codes fill and bind data to your DropDownList:

<EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"

                            DataSourceID="SqlDataSource2" DataTextField="NAME" DataValueField="BASIC_ICONURL"

                            SelectedValue='<%# Bind ("BASIC_ICONURL") %>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server"

                            ConnectionString="<%


ConnectionStrings:YourConnectionStringKey % > " span> SelectCommand =" 从[pdfmap]中选择[BASIC_ICONURL],[NAME]" < /asp:SqlDataSource > < /EditItemTemplate >
ConnectionStrings:YourConnectionStringKey %>" SelectCommand="SELECT [BASIC_ICONURL], [NAME] FROM [pdfmap]"></asp:SqlDataSource> </EditItemTemplate>




希望对您有帮助,
干杯




I hope it helps,
Cheers



好吧,正如我所说的,您需要尽快填写DropDownList.您必须使用RowCreated事件处理程序而不是RowDataBound(因为它会更快触发),因此了解Page LifeCycle和事件引发顺序非常重要.这是新代码:

Hi,
Well, as I said you need to fill your DropDownList sooner. You must use RowCreated event handler instead of RowDataBound(as it fires sooner) so it is very important to know Page LifeCycle and event raise orders. Here is the new code:

Protected Sub RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView1.EditIndex = e.Row.RowIndex Then

            Dim GV As DropDownList = e.Row.FindControl("DropDownlist1")
            connstr = "server=localhost;User Id=xxxx;password=xxx;Persist Security Info=True;database=xxx"
            strSQL = "select * from pdfmap"
            conn = New MySqlConnection(connstr)
            cmd = New MySqlCommand(strSQL, conn)
            conn.Open()
            reader = cmd.ExecuteReader
            GV.DataSource = reader
            GV.DataTextField = "NAME"
            GV.DataValueField = "BASIC_ICONURL"
            GV.DataBind()
        End If

    End Sub




我的意思是,只需将RowDataBound替换为RowCreated.它可能会起作用,因为它是主要问题.
我建议您检查GridView的不同事件处理程序.它可以帮助您实现他们的消防命令.

请随时告诉我它是否不起作用,
干杯




I mean, just replace RowDataBound with RowCreated. It will probably work as it is the main problem.
I suggest you to examine different event handlers of the GridView. It can help you realize their fire orders.

Feel free to tell me if it does not work,
Cheers


这篇关于下拉列表选定值绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆