【问题标题】:How to pass value to a dropdownlist in asp.net?如何将值传递给 asp.net 中的下拉列表?
【发布时间】:2014-01-26 11:10:22
【问题描述】:

我在 gridview 中有一个下拉列表,我的 gridview 有一个数据表数据源,当我尝试将值传递给数据源时,它不处理该值。

 SqlCommand com2 = new SqlCommand("select MRPFABRIC.GoodForHow, MRPFABRIC.MrpQty, allowance, ArticleFabricAssign.Consumption,(select SUBDETAILS.Description from SUBDETAILS where SubDetailsID = MRPFABRIC.ColorName) as Color,(select SUBDETAILS.Description from SUBDETAILS where SubDetailsID = MRPFABRIC.FabricName) as Fabric from MRPFABRIC join ARTICLE on ARTICLE.ControlNo = MRPFABRIC.ControlNo join ARTICLEFABRICASSIGN on ARTICLEFABRICASSIGN.ArtFabID = MRPFABRIC.ArtFabId join FABRIC on FABRIC.FabricID = ARTICLEFABRICASSIGN.FabricID join mrp on MRP.MRPNo = MRPFABRIC.MRPNo where MRP.MRPNo =  @fmno and ARTICLE.ArticleNo = @articleno", con);
        com2.Parameters.AddWithValue("@fmno", txtMRPNo.Text);
        com2.Parameters.AddWithValue("@articleno", ddlArtNo.SelectedItem.Text);
        DataTable dt2 = new DataTable();
        SqlDataAdapter adapt = new SqlDataAdapter(com2);
        adapt.Fill(dt2);
        GridView1.DataSource = dt2;
        GridView1.DataBind();

值的传递就在这里

 DropDownList ddlfab = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("ddlConst");
                        DropDownList ddlcol = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddlColor");
                        TextBox txtgfhm = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox5");
                        TextBox txtconsump = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox6");
                        TextBox txtallow = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("TextBox7");
                        Label lblmrpq = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label1");

                        DataRow row2 = dt2.Rows[0];
                        ddlfab.SelectedItem.Text = dt2.Rows[i][5].ToString();
                        txtgfhm.Text = dt2.Rows[i][0].ToString();
                        txtconsump.Text = dt2.Rows[i][3].ToString();
                        txtallow.Text = dt2.Rows[i][2].ToString();
                        lblmrpq.Text = dt2.Rows[i][1].ToString();
                        ddlcol.Text = dt2.Rows[i][4].ToString();

ddlcol 是不处理值的那个。 我尝试了 ddlcol.SelectedItem.Text 但它返回空引用异常,尽管我尝试传递的行有一个值。 ddlCol.SelectedValue 也不返回任何内容。

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu


    【解决方案1】:

    试试这个

            ddlcol.DataSource= dt2            
            ddlcol.DataTextField = "UserID";
            ddlcol.DataValueField = "UserID";
            ddlcol.DataBind();
    

    你真的需要检查这个link

    【讨论】:

    • 谢谢,但效果也不太好。它抓住了价值,但没有显示出来。
    • ddlArtNo 是下拉列表吗?
    • 是的,它是gridview之外的一个下拉列表,当用户选择ddlArtNo时,ddlCol必须根据ddlArtNo的值改变它的值。
    • 如果是这样你需要 com2.Parameters.AddWithValue("@articleno", ddlArtNo.SelectedItem.Value.ToString());
    • 嗯,我对源没有问题,它工作得很好,主要问题是我的 ddlCol 没有打印传递给它的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多