如何在GridView EditTemplate中设置DropDownList的SelectedValue

发布于 2024-09-14 08:10:40 字数 1193 浏览 4 评论 0原文

我正在尝试这个作为之前问过。我发现的唯一区别是上面代码中包含的附加列表项。

我尝试使用 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

I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.

I tried to use AppendDataBoundItems=true but it is still not working. I also want to set the its default value to the value that was being displayed in label of itemtemplate i.e. DropDownList's SelectedValue='<%# Eval("DepartmentName") %>' but thie property is not available to me in dropdownlist.
What could be the reason. ??

<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>   

I am using GridView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷乱花海 2024-09-21 08:10:40

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

DataValueField seems to be wrong - shouldn't it be DepartmentId? Similarly, you need to have SelectedValue='<%# Eval("**DepartmentId**") %>' - DepartmentName would be the SeletectText.

白云不回头 2024-09-21 08:10:40

使用 GridView_DataBound 事件处理程序解决了该问题。

在您的情况下,您需要添加一个 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;
    }
  }
}

The use of the GridView_DataBound event handler solves the problem.

In your case you will need to add a HiddenField to store the PK_DepartmentId value:

<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;
    }
  }
}
稚然 2024-09-21 08:10:40

为什么你们建议使用循环,当有专门为行条件变化而设计的 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;
                }
            }
        }

Why are you guys suggesting to use loops, when there is a GridView method specifically made for when a row's condition changes - the 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;
                }
            }
        }
冷…雨湿花 2024-09-21 08:10:40

这是我发现的最好的......

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;
        }
    }
}

This is the best i have found....

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;
        }
    }
}
再见回来 2024-09-21 08:10:40

在您的网格上有一个名为 ItemCommand 的事件。为其创建一个方法:

protected void Grid1_ItemCommand(object source, GridCommandEventArgs e)

现在只需创建一个 case 语句,该语句将识别用户何时单击网格上的编辑按钮:

 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 并将所选项目设置为此字符串。

On your grid there is an event called ItemCommand. Create a method for it:

protected void Grid1_ItemCommand(object source, GridCommandEventArgs e)

Now simply create a case statement that will recognize when the user has clicked the edit button on the grid:

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

Now you have a member variable set to the string you want to be selected before the dropdown is even loaded/prerendered. Use the event OnPrerender or OnLoad for the dropdownbox and set the selected item to this String.

硪扪都還晓 2024-09-21 08:10:40

在 Visual Studio 2022 中,我无法使用 asp:HiddenField,但使用隐藏的 - 效果很好。之后的一个问题是,在 RowDataBound 处理程序中,e.Row.FindControl("Count") 对于隐藏的返回 null。

我真正想要从“GridViewTest_RowDataBound”处理程序中获取与这一网格行相关的原始一行 SQL 数据。通过访问原始数据,我可以为 DropDownList 执行 FindControl,然后设置 DropDownList.SelectedIndex = iCount。

我找到了一份关于如何做到这一点的模糊参考。
从 GridViewTest_RowDataBound 处理程序中,添加以下代码:

int iCount = (int) DataBinder.Eval(e.Row.DataItem, "Count");
DropDownList DropDownListCount = (e.Row.FindControl("DropDownListCount") as DropDownList);
DropDownListCount.SelectedIndex = iCount;

在上面的示例中,“Count”是返回的 SQL 行集中所需数据列的名称。即使在之后,该代码仍然有效。被删除。

请参阅 https: //learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479342(v=msdn.10)?redirectedfrom=MSDN 使用 DataBinder 的 Microsoft 示例。

In Visual Studio 2022, I wasn't able to use a asp:HiddenField, but using a <asp:BoundField DataField="Count"> that was hidden - worked out well. One problem after that was that from the RowDataBound handler, e.Row.FindControl("Count") returned null for the hidden <asp:BoundField DataField="Count">.

What I really wanted to do from the "GridViewTest_RowDataBound" handler was to get at the original one row of SQL DATA that was related to this one Grid row. With access to that original data, I could do a FindControl for the DropDownList and then set DropDownList.SelectedIndex = iCount.

I found one obscure reference for how to do that.
From your GridViewTest_RowDataBound handler, add the following code:

int iCount = (int) DataBinder.Eval(e.Row.DataItem, "Count");
DropDownList DropDownListCount = (e.Row.FindControl("DropDownListCount") as DropDownList);
DropDownListCount.SelectedIndex = iCount;

In the example above, "Count" is the name of the desired data column in the SQL rowset returned. That code works even after the <asp:BoundField DataField="Count"> is removed.

See https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479342(v=msdn.10)?redirectedfrom=MSDN for a Microsoft example using DataBinder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文