【问题标题】:Update GridView with Dropdownlist control使用下拉列表控件更新 GridView
【发布时间】:2011-12-06 07:17:47
【问题描述】:

我正在使用 VS2005 C#。

目前我有一个 GridView,并且我已将我的 GridView 控件之一更改为我的列名Gender,从默认的TextBox 更改为DropDownList,我将控件的ID 设置为@ 987654327@,它包含2个值,MF

我有一个默认的更新语句,它能够在edit 之后更新 GridView,如下所示:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=
"<%$ ConnectionStrings:SODConnectionString %>" UpdateCommand="UPDATE 
[UserMasterData] SET [Name] = @Name, [Age] = @Age, [ContactNo]=@ContactNo,
 [Address]=@Address, [Gender]=@Gender"/>

上面的 UPDATE 查询完美运行,现在我已将我的 Gender textbox 更改为 dropdownlistUPDATE 查询给了我一个错误,上面写着:

Must declare the scalar variable "@Gender".

我假设 UPDATE 查询无法从我的 Gender 列中找到值。

我尝试将 UPDATE 查询修改为 @GenderList,但效果不佳。

任何人都知道我应该做什么来执行 UPDATE 查询,以便我的 UPDATE 查询可以从我的 GenderList 下拉列表中的 Gender 列中找到值?

谢谢。


下面是我之前的Gender 列,带有一个文本框控件:

                <asp:BoundField  HeaderText="Gender" 
                DataField="Gender" 
                SortExpression="Gender"></asp:BoundField>

下面是我的Gender,带有下拉列表控件:

    <asp:TemplateField HeaderText="Gender" SortExpression="Gender" >
        <EditItemTemplate>
            <asp:DropDownList ID="GenderList" runat="server" Width="50px" >
                <asp:ListItem>M</asp:ListItem>
                <asp:ListItem>F</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>

编辑:

尝试实现RowDatBoundonRowUpdating


RowDatBound


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView dRowView = (DataRowView)e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList genderList= (DropDownList)e.Row.FindControl("GenderList");
            genderList.SelectedValue = dRowView[2].ToString();
        }
    }
}

RowUpdating.aspx.cs


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

DropDownList genderSelect =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("GenderList");

SqlDataSource1.UpdateParameters["Gender"].DefaultValue = 
genderSelect.SelectedValue; --> error says not set to an instance of an object

}

【问题讨论】:

  • stackoverflow.com/questions/8395365/… 同样的问题还有一个新的ACC ???不要用新线程问同样的问题,即使是你的同一个用户stackoverflow.com/users/872370/ruihao
  • 请不要创建新帐户并再次提出相同的问题。另外,不要在你的问题前面加上“asp.net”。无需在标题中添加标签;我们有一个运行良好的标签系统!

标签: c# asp.net sql


【解决方案1】:

如果您使用 SqlDataSource 并通过它更新数据,那么您所要做的就是为下拉列表 GenderList 设置 2 路绑定。

您可以通过设计器或直接在源代码中进行设置

 <EditItemTemplate>
                <asp:DropDownList ID="GenderList" runat="server" 
                    SelectedValue='<%# Bind("Gender") %>'>
                    <asp:ListItem>M</asp:ListItem>
                    <asp:ListItem>F</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>

注意这里使用了 2 路绑定。

【讨论】:

  • 如何动态填充这个下拉列表?我应该在每个 RowDataBound 事件中都这样做吗?以及如何使用文本值(而不是“性别”值)填充 ?谢谢
【解决方案2】:

您需要使用其 selected value 属性,下拉列表是值的集合,但您正在为更新语句分配一个标量值。 您必须检查它何时处于更新模式,您需要获取下拉列表的选定项值并将该值用作更新语句的参数。
Editing with Dropdownlist in gridview

在编辑模式下检查它还是不在 RowDataBound 事件中这样使用

if ((e.Row.RowState & DataControlRowState.Edit) > 0)

【讨论】:

  • 感谢您的链接。我试过了,但遇到了一些错误,指出我的引用未设置为对象实例。
  • 检查你是否正确地实例化了你的对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多