2010-08-13 45 views
8

我想按照前面的方法做this。我发现的唯一区别是上面代码中包含的附加列表项。如何在GridView的EditTemplate中设置DropDownList的SelectedValue

我试图使用AppendDataBoundItems=true,但它仍然无法正常工作。我还希望将其默认值设置为在itemtemplate的标签中显示的值,即DropDownList的SelectedValue='<%# Eval("DepartmentName") %>',但是在下拉列表中,我的属性不可用。 可能是什么原因。 ??

<EditItemTemplate> 
    <asp:DropDownList ID="ddlDepartment_Edit" runat="server" 
     DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName" 
     DataValueField="PK_DepartmentId"> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" 
     ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>" 
     ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" 
     SelectCommandType="StoredProcedure"> 
    </asp:SqlDataSource>         
</EditItemTemplate> 
<ItemTemplate> 
    <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' > 
    </asp:Label> 
</ItemTemplate> 

我使用GridView

回答

8

DataValueField似乎是错的 - 它不应该是DepartmentId?同样,您需要有SelectedValue='<%# Eval("**DepartmentId**") %>' - DepartmentName将是SeletectText

+2

这是我陷入困境的地方。没有这样的属性对我来说是可见的,即SelectedValue或SelectedText,以便我可以为它们分配一些值。关于DataValueField,是的,你是对的,它应该是主键。但直到这两个属性可用于我之前,不是讨论的一部分。 – 2010-08-13 10:28:48

+2

VS设计师可能不会以智能的方式显示您的财产,但它的存在。有两个属性 - SelectedValue和SelectedIndex。如果您编写SelectedIndex ='<%#Eval('DepartmentId')%>'会发生什么? – VinayC 2010-08-13 11:29:11

+0

我的错误 - 应该是SelectedValue ='<%#Eval('DepartmentId')%>'。 – VinayC 2010-08-13 11:37:43

0

在您的网格上有一个事件叫做ItemCommand。为它创建一个方法:

protected void Grid1_ItemCommand(object source, GridCommandEventArgs e) 

现在简单地创建,当用户点击了电网的编辑按钮,将认识的情况下声明:

case Grid.EditCommandName:  
//set a member variable to the string of the cell you are editing. 
//something like: mString = e.item..["Column"].toString();     
        break; 

现在你有一个成员变量设置为要在下拉列表被加载/预渲染之前选择字符串。对dropdownbox使用事件OnPrerenderOnLoad,并将所选项目设置为此字符串。

3

事件处理函数的使用解决了这个问题。

在你的情况,你将需要添加一个HiddenField存储PK_DepartmentId值:

<asp:GridView ID="gvExample" runat="server" AutoGenerateColumns="False" OnDataBound="gvExample_DataBound"> 
    <Columns> 
    <asp:TemplateField HeaderText="Department"> 
     <EditItemTemplate> 
     <asp:DropDownList ID="ddlDepartment_Edit" runat="server" DataSourceID="dsDepartment_Edit" 
      DataTextField="DepartmentName" DataValueField="PK_DepartmentId"> 
     </asp:DropDownList> 
     <asp:HiddenField ID="hfDepartmentId" runat="server" Value='<%# Bind("PK_DepartmentId") %>' /> 
     <asp:SqlDataSource ID="dsDepartment_Edit" runat="server" ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>" 
      ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" SelectCommandType="StoredProcedure"> 
     </asp:SqlDataSource> 
     </EditItemTemplate> 
     <ItemTemplate> 
     <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>'> 
     </asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:CommandField ShowEditButton="True" ButtonType="Button" /> 
    </Columns> 
</asp:GridView> 

protected void gvExample_DataBound(object sender, EventArgs e) 
{ 
    foreach (GridViewRow gvRow in gvExample.Rows) 
    { 
    DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList; 
    HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField; 

    if (ddlDepartment != null && hfDepartmentId != null) 
    { 
     ddlDepartment.SelectedValue = hfDepartmentId.Value; 
    } 
    } 
} 
+0

谢谢!解决了这个问题 – 2016-06-09 15:56:54

0

这是最好的我已经找到....

protected void GridView1_DataBound(object sender, EventArgs e) 
{ 
    foreach (GridViewRow gvRow in GridView1.Rows) 
    { 
     RadioButtonList rbl = gvRow.FindControl("rblPromptType") as RadioButtonList; 
     HiddenField hf = gvRow.FindControl("hidPromptType") as HiddenField; 

     if (rbl != null && hf != null) 
     { 
      if (hf.Value != "") 
      { 
       //clear the default selection if there is one 
       rbl.ClearSelection(); 
      } 

      rbl.SelectedValue = hf.Value; 
     } 
    } 
} 
0

为什么你们建议要使用循环,当有一个GridView方法专门用于当行的条件发生变化时 - RowDataBound()

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      GridViewRow gvRow = (GridViewRow)e.Row; 
      HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID"); 
      if (hfAgentID != null) 
      { 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent"); 
        ddlAgent.SelectedValue = hfAgentID.Value; 
       } 
      } 
     } 
相关问题