在GridView中选择行时,需要有关填充DropDownList的帮助 [英] Need help on populating DropDownList when selecting a row in GridView

查看:58
本文介绍了在GridView中选择行时,需要有关填充DropDownList的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须使用三个下拉列表,第二个是从第一个中选择的项目填充。

当我从GridView中选择一行时,问题就存在了。我可以将这些值发送到dropdownlist,但是如果我在gridview中选择一行,则第一个DropDownList不会填充第二个值

第一个DDL的属性设置为IsPostBack,因为当我选择一个Item时,它必须填充第二个DDL。

代码如下:

DDL1

  public   void  ShowDDL1_Items()
{

SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
尝试
{

使用(sqlConn)
{

SqlCommand sqlCmd = new SqlCommand( procDDL1,sqlConn);


sqlCmd.CommandType = CommandType.StoredProcedure;


sqlCmd.Parameters.Add( new SqlParameter( @ ID_DDL1,SqlDbType.Int));


sqlCmd.Parameters [ @ ID_DDL1]。 Value = Convert.ToInt32(Session [ ID_Shkolla]);


sqlConn.Open();


SqlDataReader reader = sqlCmd.ExecuteReader();


if (reader.HasRows)
{
DDL1.DataSource = reader;
DDL1.DataTextField = FieldName;
DDL1.DataValueField = ID_Field;
DDL1.DataBind();
DDL1.Items.Insert( 0 new ListItem( - 选择 - 0));
DDL2.Items.Insert( 0 new ListItem( - 选择 - 0));
DDL3.Items.Insert( 0 new ListItem( - 选择 - 0));
}
}
}
catch (例外ex
{
throw ex;
}
finally
{
sqlConn.Close( );
}
}





DDL2

< pre lang =c#> protected void DDL1_SelectedIndexChanged( object sender,EventArgs e)
{

SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
尝试
{

使用(sqlConn)
{

SqlCommand sqlCmd = new SqlCommand( procDDL2,sqlConn);


sqlCmd.CommandType = CommandType.StoredProcedure;


sqlCmd.Parameters.Add( new SqlParameter( @ ID_Field,SqlDbType.Int));


sqlCmd.Parameters [ @ ID_Field]。 Value = Convert.ToInt32(ddListDrejtimi.SelectedValue);


sqlConn.Open();


SqlDataReader reader = sqlCmd.ExecuteReader();


if (reader.HasRows)
{
DDL2.DataSource = reader;
DDL2.DataTextField = DDL2FieldName;
DDL2.DataValueField = ID_Field2;
DDL2.DataBind();
DDL2.Items.Insert( 0 new ListItem( - 选择 - 0));
}
}
}
catch (例外情况)
{
throw ex;
}
最后
{
sqlConn.Close();
}
}





如果我在gridview中选择一行,如何填充DD2

  protected   void  GV1_SelectedIndexChanged( object  sender,EventArgs e)
{

DDL1.SelectedValue = GV1.SelectedRow.Cells [ 5 ]文本;
DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells [ 6 ]。Text.ToString());}

/// 如果我为DDL2选择一列,它告诉我DDL2中不存在该值,这意味着DDl2未填充





提前感谢您的回复。

解决方案

尝试调用DDL1 SelectedIndexChanged的事件处理程序。

类似于 -

 受保护  void  GV1_SelectedIndexChanged(对象发​​件人,EventArgs e)
{
// 现有代码
DDL1.SelectedValue = GV1.SelectedRow.Cells [ 5 ]。文字;
DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells [ 6 ]。Text.ToString());
// 添加此行
DDL1_SelectedIndexChanged(DDL1,EventArgs.Empty);
}





希望,它有帮助:)



注意:@Richard在评论中建议更新。


解决了问题。如果其他人有同样的问题,我正在编写下面的代码来解决问题

  protected   void  GV1_SelectedIndexChanged( object  sender,EventArgs e)
{
// 此行获取ID 的列
DDL1.SelectedValue = GV1.SelectedRow.Cells [ 5 ]文本。
/// 此代码调用事件
DDL1_SelectedIndexChanged( this ,EventArgs.Empty);

}





感谢所有帮助过我的人。

干杯。


On my project I have to use three dropdownlists and the second one is populated from item selected from first one.
The problem exists there while I'm selecting a row from GridView. I can send those values to dropdownlist but the first DropDownList is not populating the second one if I select a row in gridview
The property of first DDL is set to IsPostBack because when I select an Item, it must populate the second DDL.
The code is as following:
DDL1

public void ShowDDL1_Items()
       {

           SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
           try
           {

               using (sqlConn)
               {

                   SqlCommand sqlCmd = new SqlCommand("procDDL1", sqlConn);


                   sqlCmd.CommandType = CommandType.StoredProcedure;


                   sqlCmd.Parameters.Add(new SqlParameter("@ID_DDL1", SqlDbType.Int));


                   sqlCmd.Parameters["@ID_DDL1"].Value = Convert.ToInt32(Session["ID_Shkolla"]);


                   sqlConn.Open();


                   SqlDataReader reader = sqlCmd.ExecuteReader();


                   if (reader.HasRows)
                   {
                       DDL1.DataSource = reader;
                       DDL1.DataTextField = "FieldName";
                       DDL1.DataValueField = "ID_Field";
                       DDL1.DataBind();
                       DDL1.Items.Insert(0, new ListItem("--Select--", "0"));
                       DDL2.Items.Insert(0, new ListItem("--Select--", "0"));
                       DDL3.Items.Insert(0, new ListItem("--Select--", "0"));
                   }
               }
           }
           catch (Exception ex
           {
               throw ex;
           }
           finally
           {
               sqlConn.Close();
           }
       }



The DDL2

protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
       {

           SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
           try
           {

               using (sqlConn)
               {

                   SqlCommand sqlCmd = new SqlCommand("procDDL2", sqlConn);


                   sqlCmd.CommandType = CommandType.StoredProcedure;


                   sqlCmd.Parameters.Add(new SqlParameter("@ID_Field", SqlDbType.Int));


                   sqlCmd.Parameters["@ID_Field"].Value = Convert.ToInt32(ddListDrejtimi.SelectedValue);


                   sqlConn.Open();


                   SqlDataReader reader = sqlCmd.ExecuteReader();


                   if (reader.HasRows)
                   {
                       DDL2.DataSource = reader;
                       DDL2.DataTextField = "DDL2FieldName";
                       DDL2.DataValueField = "ID_Field2";
                       DDL2.DataBind();
                       DDL2.Items.Insert(0, new ListItem("--Select--", "0"));
                   }
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               sqlConn.Close();
           }
       }



How to populate DD2 if I select a row in gridview

 protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
        {

            DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
            DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells[6].Text.ToString());}

///If I select a column for DDL2 it tells me that that value does not exist in DDL2 which means the DDl2 is not populated



Thank you in advance for your reply.

解决方案

Try calling the event handler of DDL1 SelectedIndexChanged.
Something like-

protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //your existing code
            DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
            DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells[6].Text.ToString());
            //add this line
            DDL1_SelectedIndexChanged(DDL1, EventArgs.Empty);
}



Hope, it helps :)

Note: Updated as suggested by @Richard in the comments.


Solved the problem. In case that someone else has the same problem I'm writing the code below to solve the problem

protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this row gets the column with ID
            DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
            ///this code calls the event
            DDL1_SelectedIndexChanged(this, EventArgs.Empty);

            }



Thanks to all who helped me.
Cheers.


这篇关于在GridView中选择行时,需要有关填充DropDownList的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆