如何通过单击gridview控件来替换下拉列表值 [英] how to replace the dropdownlist value by clicking from gridview control

查看:77
本文介绍了如何通过单击gridview控件来替换下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编辑网格视图值并使用下拉列表更新值。

这意味着当我单击网格视图中的编辑按钮时,特定行的值将被放置在下拉列表..

i wanna edit the grid view value and update the value by using drop downlist..
it means when i click edit button in grid view ,the value of the particular row to be place at the drop down list..

推荐答案

请参阅以下代码:



Refer to below code:

<asp:templatefield headertext="names" headerstyle-horizontalalign="Center" xmlns:asp="#unknown">
  <itemtemplate>
  <asp:label id="Name_label" runat="server" text="<%# Bind("name") %>"></asp:label>
  </itemtemplate>
  <HeaderStyle HorizontalAlign="Center" />
       <itemstyle horizontalalign="Center" />
  <edititemtemplate>
   <asp:dropdownlist runat="server" id="name_list" datatextfield="name" datavaluefield="name">
   </asp:dropdownlist>
  </edititemtemplate>
</asp:templatefield>





这里我们将标签视为项目模板和下拉列表作为编辑itemtemplate,例如它将显示si的名称ngle学生处于正常模式(标签),但是当您点击编辑时,它将获得包含班级中所有学生姓名的下拉列表。



现在,这将是此操作的代码:





Here we have taken label as item template and dropdown list as a edit itemtemplate,that is for example it will display the name of the single student in normal mode(label),but as and when you click the edit,it will get the dropdown list with names of all the students in the class.

Now,this would be the code for this operation:

protected void yourgird_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["your connection string"].ConnectionString;
  conn.Open();
  Label lblID1 = yourgrid.Rows[e.RowIndex].FindControl("anyuniquerow") as Label;
  DropDownList namelist_temp= yourgrid.Rows[e.RowIndex].FindControl("name_list") as DropDownList;
  SqlCommand cmd = new SqlCommand("update student_details set name='" + namelist_temp.SelectedValue + "' where code='" + lblID1.Text + "'", conn);//here use parameterized query
  cmd.ExecuteNonQuery();
  yourgrid.EditIndex = -1;
  fillrole();//this function is binding the gridview
}
protected void yourgrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow &&
    (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
    {
       // Here you will get the Control you need like:
                DropDownList dl = (DropDownList)e.Row.FindControl("name_list");
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["your connection string"].ConnectionString;
                conn.Open();
                SqlCommand cmd = new SqlCommand("select name from student_details", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dl.DataSource = ds;
                dl.DataBind();
                conn.Close();
            }

        }
        protected void yourgrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            yourgrid.EditIndex = e.NewEditIndex;
            fillrole();
        }





别忘了在页面的gridview定义中添加所有这些事件。尝试理解代码。这是我在很多项目中使用的代码。



问候..:笑:



Dont forget to add all this events in gridview definition at the page. Try to understand the code. This is the code i used in many project.

Regards.. :laugh:


最后,我得到了答案



参考下面的代码

代码应该是gridview中的图像按钮。

first

//获取网格视图控件



使用(gridviewrow row =(gridviewrow)((linkbutton)sender).parent.parent)

{





label newobj =(label)row.findcontrol(Label1);



// Label1是绑定网格的控件的labelID



//< asp:templatefield headertext =类别xmlns:asp =#unknown> <&的ItemTemplate GT; < asp:label id =Label1runat =servertext =<%#Eval(category_name)=%& gt;=>





//这可能在您绑定网格的设计页面中?





ddlcategory.Items.FindByText(((Label)row.FindControl(Label1))。Text).Selected = true;





//在此绑定网格之前

}



谢谢,
Finally ,i got the answer

refer the below code
the code should be image button with in gridview.
first
//get the grid view controls

using (gridviewrow row = (gridviewrow)((linkbutton)sender).parent.parent)
{


label newobj=(label)row.findcontrol("Label1");

//Label1 is the labelID of the control where you bind the Grid

// <asp:templatefield headertext="Category" xmlns:asp="#unknown"> <itemtemplate> <asp:label id="Label1" runat="server" text="<%#Eval(" category_name")="" %&gt;"="">


//this may be in your design page where you bind the Grid??


ddlcategory.Items.FindByText(((Label)row.FindControl("Label1")).Text).Selected = true;


//before this bind the Grid
}

thank you,


这篇关于如何通过单击gridview控件来替换下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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