0

gridview中下拉菜单的源代码

     <asp:TemplateField HeaderText="LocationServerName" HeaderStyle-BackColor="#C0C0C0" HeaderStyle-BorderColor="Black">
    <ItemTemplate>
    <asp:TextBox ID="txtLocationServerName" runat="server" OnTextChanged="txtLocationServerName_TextChanged"  ReadOnly="true" Text='<%# Eval("LocationServerName")%>' > </asp:TextBox>
     </ItemTemplate>                  
    </asp:TemplateField>
    <asp:TemplateField HeaderText="ProjectModifiedBy" HeaderStyle-BackColor="#C0C0C0" HeaderStyle-BorderColor="Black">
    <ItemTemplate>
     <asp:DropDownList ID="ddlProjectModifiedBy" Width="99%" runat="server" Enabled="false" 
            onselectedindexchanged="ddlProjectModifiedBy_SelectedIndexChanged" AutoPostBack="true" >
      </asp:DropDownList>
       </ItemTemplate> 
        </asp:TemplateField>

我的代码将下拉列表与gridview绑定。但是gridview没有显示gridview中下拉列表的选定值,将数据保存到数据库后。如何解决这个问题?

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

    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim retDt = New DataTable
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Dim sqlQuery As String

    sqlQuery = "Sp_SelectEmpName"
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand(sqlQuery, con)
    Dim da = New SqlDataAdapter(cmd)
    cmd.CommandType = CommandType.StoredProcedure
    da.Fill(retDt)
    Dim dropdown = TryCast(e.Row.FindControl("ddlProjectModifiedBy"), DropDownList)

    dropDown.DataSource = retDt
    dropDown.DataValueField = "EmpId"
    dropDown.DataTextField = "EmpName"
    dropdown.DataBind()

    End If
End Sub
4

2 回答 2

0
<ItemTemplate>
  <asp:DropDownList ID="ddlProjectModifiedBy" Width="99%" runat="server" Enabled="false" 
  onselectedindexchanged="ddlProjectModifiedBy_SelectedIndexChanged" AutoPostBack="true" >
  </asp:DropDownList>
</ItemTemplate> 

在代码中添加编辑模板

<EditTemplate>
        <asp:DropDownList ID="ddlProjectModifiedBy" Width="99%" runat="server" Enabled="false" 
  onselectedindexchanged="ddlProjectModifiedBy_SelectedIndexChanged" AutoPostBack="true" >
  </asp:DropDownList>
</EditTemplate> 

在您的 RowDataBoound 事件中添加此代码

If e.Row.RowType = DataControlRowType.DataRow AndAlso dgrd_WWWH_How.EditIndex = e.Row.RowIndex Then
  Dim ddlresp As DropDownList = CType(e.Row.FindControl("ddlResp"), DropDownList)
    'Here you get your current value from db to store any string   
     Dim lblid as Label = CType(e.Row.Fincontrol("id"),Label)
    strresp = db.getvalue(select respval from tbl where id =lblid)
   ddlresp.Items.FindByText(strresp).Selected = True
End If
于 2013-11-15T11:25:02.007 回答
0

您没有将下拉列表绑定到您的数据

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

If e.Row.RowType = DataControlRowType.DataRow Then
    Dim retDt = New DataTable
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Dim sqlQuery As String

    sqlQuery = "Sp_SelectEmpName"
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand(sqlQuery, con)
    Dim da = New SqlDataAdapter(cmd)
    cmd.CommandType = CommandType.StoredProcedure
    da.Fill(retDt)
    Dim dropdown = TryCast(e.Row.FindControl("ddlProjectModifiedBy"), DropDownList)

    dropDown.DataSource = retDt
    dropDown.DataValueField = "EmpId"
    dropDown.DataTextField = "EmpName"
    dropdown.DataBind()

    'I don't know the Type of your Data...
    ' next lines could be wrong just replace it with the right datatypes
    Dim dr As DataRow = CType(e.Row.DataItem, DataRowView).Row
    Try
        dropdown.SelectedValue = dr("dropDownselectedValue")
    Catch ex As Exception

    End Try
End If

结束子

于 2013-11-15T11:28:23.700 回答