如何在edititemtemplate下拉列表中将itemtemplate标签文本显示为具有其他数据库值的选定值? [英] how to show itemtemplate label text in edititemtemplate dropdownlist as selected value with other database values?
问题描述
亲爱的所有人,
我在itemtemplate和edititemtemplate中使用标签,我正在使用dropdownlist。当我点击gridview中的编辑链接时,我想显示带有标签文本的Dropdownlist作为选定值和其他数据库值。我尝试了很多解决方案,但没有奏效。我已经设置了autopostback true的下拉列表。这是我的代码 -
protected void gvTransport_OnRowDataBound(object sender,GridViewRowEventArgs e)
{
if( e.Row.RowType == DataControlRowType.DataRow && gvTransport.EditIndex == e.Row.RowIndex)
{
/ * ...用于从dbo获取路由名称.Transport_Routes表... * /
DropDownList RouteName =(DropDownList)e.Row.FindControl(ddlRoute_ID);
Label lblRName =(Label)e.Row .FindControl(lblRoute_ID);
RouteName.DataSource = LoadStudentRoutes(); //获取路径名称
RouteName.DataTextField =Route_Name;
RouteName.DataValueField =Route_ID;
RouteName.DataBind( );
RouteName.Items.FindByText(lblRName.Text).Selected = true;
}
}
但是点击编辑链接时不会触发编辑事件。在这种情况下请帮帮我。
谢谢和问候,
Rizwan Gazi。
Dear All,
I am using label in itemtemplate and in edititemtemplate, I am using dropdownlist. When I click on edit link in gridview, I want to show Dropdownlist with label's text as selected value with other database values. I tried many solutions, but didn't work. I have set autopostback true of dropdown. Here is my code -
protected void gvTransport_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvTransport.EditIndex == e.Row.RowIndex)
{
/*... For getting Route Names from dbo.Transport_Routes table ...*/
DropDownList RouteName = (DropDownList)e.Row.FindControl("ddlRoute_ID");
Label lblRName = (Label)e.Row.FindControl("lblRoute_ID");
RouteName.DataSource = LoadStudentRoutes(); // To Get Route Names
RouteName.DataTextField = "Route_Name";
RouteName.DataValueField = "Route_ID";
RouteName.DataBind();
RouteName.Items.FindByText(lblRName.Text).Selected = true;
}
}
but edit event is not fired, when I click edit link. Please help me in this situation.
Thanks and regards,
Rizwan Gazi.
推荐答案
嗨Rizwan,以下是解决方案: -
1.首先,将下拉列表的自动后备设置为False
2.使用RowCommand事件网格视图,将编辑标签的值获取到ViewState中,如下所示: -
Hi Rizwan, Here is the solution:-
1. Firstly, Set autopostback of dropdownlist to "False"
2. With the use of RowCommand Event of the Grid View , Get the value of the Edited Label into the ViewState, As below:-
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridViewRow gvRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Label lbl = (Label)gvRow.FindControl("Label1");
ViewState["LabelValue"] = lbl.Text;
}
}
3.最后,使用viewstate并处理你的RowDataBound事件如下: -
3. Finally, Use viewstate and Handle your RowDataBound Event as below :-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
//Label lbl = (Label)e.Row.FindControl("Label1");
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
ddl.SelectedValue = ddl.Items.FindByText(Convert.ToString(ViewState["LabelValue"])).Value;
}
}
4.你的工作已经完成!!!
~谢谢
4. Your Job Is done !!!
~Thanks
这篇关于如何在edititemtemplate下拉列表中将itemtemplate标签文本显示为具有其他数据库值的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!