Hi localhost,
Welcome to Microsoft Newsgroup Service. Based on your problem description,
you have a DataGrid with a Template column, in which, the ItemTemplate is a
Lable and the EditItemTemplate is a DropDownList, and you want to set the
DropDownList's selectedIndex according to the value when show in the
Label(not in edit mode), is my understanding correct?
I've tested the code you provided, I found that there are something
incorrect when run it. Such as :
DataRowView ddView = eA.Item.DataItem;
DropDownList ddTemp = eA.Item.FindControl
( "ddnChoiceId" );
the DropDownList hasn't been created at the DataGrid's EditCommand event,
so we are unable to retrieve it using the "FindControl". Don't worry,
though we can't retrieve the DropDownList in the DataGrid's EditCommand
event, there is another way we initialize its value. You can specify a
"OnLoad" event handler for the DropDownList, for example:
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"gender") %>
</ItemTemplate>
<EditItemTemplate>
<asp
ropDownList ID="lstGender" OnLoad="lstGender_Load" Runat="server">
<asp:ListItem Value="male">Male</asp:ListItem>
<asp:ListItem Value="female">Female</asp:ListItem>
</asp
ropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
Then, implement this handler function("lstGender_Load") in the Page Class:
protected void lstGender_Load(object source, System.EventArgs e)
{
DropDownList lstGender = (DropDownList)source;
//get the datasource of the DataGrid(you should change it to other
//if you don't use DataTAble as the datasource)
DataTable tb = (DataTable)gridTest.DataSource;
DataRow row = tb.Rows[gridTest.EditItemIndex];
if(row["gender"].Equals("male"))
{
lstGender.SelectedIndex = 0;
}
else
{
lstGender.SelectedIndex = 1;
}
}
Then, in the DataGrid's EditCommand event, you just need to set its
EditItemIndex and rebind the data:
private void gridTest_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
gridTest.EditItemIndex = e.Item.ItemIndex;
gridTest.DataSource = Get_Data();//Get_Data() returns a DataTable for
testing
gridTest.DataBind();
}
#notice that the "gridTest_EditCommand" is called before the
"lstGender_Load", that also indicate that the DropDownList hasn't been
created when the DataGrid_EditCommand event is fired.
Please try the preceding suggestion and let me know whether they help.
Below is my test page and its page class's source
----------------DataGridEdit.aspx-----------
<%@ Page language="c#" Codebehind="DataGridEdit.aspx.cs"
AutoEventWireup="false" Inherits="MyWebApp.DataGridEdit" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridEdit</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="
http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><asp:datagrid id="gridTest" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"id") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"name") %>
</ItemTemplate>
<EditItemTemplate>
<input type="text" value='<%#
DataBinder.Eval(Container.DataItem,"name") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"email") %>
</ItemTemplate>
<EditItemTemplate>
<input type="text" value='<%#
DataBinder.Eval(Container.DataItem,"email") %>'/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"gender") %>
</ItemTemplate>
<EditItemTemplate>
<asp
ropDownList ID="lstGender" OnLoad="lstGender_Load"
Runat="server">
<asp:ListItem Value="male">Male</asp:ListItem>
<asp:ListItem Value="female">Female</asp:ListItem>
</asp
ropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid></td>
</tr>
<tr>
<td><FONT face="ËÎÌå">
<asp:TextBox id="txtTest" runat="server"></asp:TextBox></FONT>
</td>
</tr>
</table>
</form>
</body>
</HTML>
---------------DataGridEdit.aspx.cs-----------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MyWebApp
{
/// <summary>
/// Summary description for DataGridEdit.
/// </summary>
public class DataGridEdit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtTest;
protected System.Web.UI.WebControls.DataGrid gridTest;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
gridTest.DataSource = Get_Data();
gridTest.DataBind();
}
}
protected DataTable Get_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("id");
tb.Columns.Add("name");
tb.Columns.Add("email");
tb.Columns.Add("gender");
for(int i=0;i<20;i++)
{
DataRow row = tb.NewRow();
row["id"] = i+1;
row["name"] = "Name" + i.ToString();
row["email"] = "Email" + i.ToString();
if(i%2 == 0)
{
row["gender"] = "male";
}
else
{
row["gender"] = "female";
}
tb.Rows.Add(row);
}
return tb;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.gridTest.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.gridTest_EditComm
and);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected void lstGender_Load(object source, System.EventArgs e)
{
DropDownList lstGender = (DropDownList)source;
DataTable tb = (DataTable)gridTest.DataSource;
DataRow row = tb.Rows[gridTest.EditItemIndex];
if(row["gender"].Equals("male"))
{
lstGender.SelectedIndex = 0;
}
else
{
lstGender.SelectedIndex = 1;
}
}
private void gridTest_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
gridTest.EditItemIndex = e.Item.ItemIndex;
gridTest.DataSource = Get_Data();
gridTest.DataBind();
}
}
}
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)