请教高手们,关于DataGrid中自定义模板列的问题!

basten_chenbin 2002-05-10 08:24:51
我想在DataGrid中作一个模板列,这个列在读模式下是一个TextBox控件,在编辑模式下是一个DropDownList控件,并且其列表项要从数据库中得到,应该如何完成?
...全文
99 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
basten_chenbin 2002-05-12
  • 打赏
  • 举报
回复
我照前两种方法作,结果在编辑状态下,dropdownlist中的每个item的值都是空字符串,怎么回事啊?
jiseng 2002-05-11
  • 打赏
  • 举报
回复
经典!鼓掌!
andy_123 2002-05-11
  • 打赏
  • 举报
回复
看我的:先定义用户控件 usercontrol.ascx(使用xml和使用数据库一样道理)
<%@ import namespace="system.data"%>
<script language="vb" runat=server>
Dim xmlds As DataSet=new dataset()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
xmlds.ReadXml(Server.MapPath("new_class.xml"))
drop.DataSource=xmlds
drop.DataBind()
End Sub

</script>
<html>
<body>
<select id="drop" DataTextField="classname" runat=server/>
</body>
</HTML>
然后在另一个aspx文件调用:
<%@ register tagprefix="MyControl" tagname="dropdown" src="usercontrol.ascx"%>
......
datagrid中:
<asp:TemplateColumn HeaderText="header">
<ItemTemplate><%#container.dataitem("dataname")%></ItemTemplate>
<EditItemTemplate>
<MyControl:dropdown id=dropdown1 runat="server"></MyControl:dropdown>
</EditItemTemplate>
</asp:TemplateColumn>


MFC2001 2002-05-11
  • 打赏
  • 举报
回复
这个问题老外早想到了,看看这个吧!

How do I add a DropDownList to EditItemTemplate, using an Access database, and then some?

By: Donny Mack
Level: Beginner
Posted Date: 7/24/2001
Tested with ASP.NET Beta 2 (v.1.0.2914.16)
Click for Printable Version
Click here to download sample code
Member Rating: 3.50 (Rated by 2 members)
Rate This Item

--------------------------------------------------------------------------------
In a recent artlicle we discussed how to populate a DropDownList box from within a DataGrid's Column while the DataGrid was in "edit mode". Well, this article builds off that example, and adds some new elements and features. First, instead of using the SQL we are going to use Access - specifically, the Northwind database. Second, when filling the DropDownList we aren't going to make a seperate call to the database to get the data - instead we are going to use a DataView and filter out the specific rows we need to populate each individual DropDownList. Third, we are going to save the changes back to Access once they have been edited.
Listing 1.1 contains the code we are going to be working with for this example:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script language="c#" runat="server">

OleDbDataAdapter OleDbDa;
OleDbConnection SqlCon;
DataView dvProducts;
DataSet ds;

protected void Page_Load(Object sender, EventArgs e){

string path = Request.MapPath("nwind.Mdb");
SqlCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" + path);

if (! IsPostBack){

BindGrid();

}

}

protected void BindGrid(){

OleDbDa = new OleDbDataAdapter("SELECT S.*, (SELECT COUNT(*) FROM Products AS P WHERE P.SupplierID = S.SupplierID) AS [ProductCount] FROM Suppliers AS S", SqlCon);
ds = new DataSet();
OleDbDa.Fill(ds, "Suppliers");
OleDbDa.SelectCommand = new OleDbCommand("SELECT ProductName, ProductID, SupplierID FROM Products", SqlCon);
OleDbDa.Fill(ds, "Products");
dvProducts = ds.Tables["Products"].DefaultView;
MyDataGrid.DataSource = ds.Tables["Suppliers"];
Page.DataBind();

}

protected DataView GetProducts(int SupplierID){

dvProducts.RowFilter = "SupplierID = " + SupplierID;
return dvProducts;

}

protected void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) {

MyDataGrid.EditItemIndex = e.Item.ItemIndex;
BindGrid();

}

protected void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) {

MyDataGrid.EditItemIndex = -1;
BindGrid();

}

protected void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e) {

string ChangeProductNameFrom = (string)((DropDownList)(e.Item.FindControl("edit_Product"))).SelectedItem.Text;
string ChangeProductNameTo = (string)((TextBox)(e.Item.FindControl("NewProductName"))).Text;
int ProductID = int.Parse((string)((DropDownList)(e.Item.FindControl("edit_Product"))).SelectedItem.Value);
OleDbCommand odbcommand = new OleDbCommand("UPDATE Products SET ProductName = @ProductName WHERE ProductID = @ProductID", SqlCon);
odbcommand.Parameters.Add(new OleDbParameter("@Productname", OleDbType.VarChar, 500));
odbcommand.Parameters["@ProductName"].Value = ChangeProductNameTo;
odbcommand.Parameters.Add(new OleDbParameter("@ProductID", OleDbType.Integer));
odbcommand.Parameters["@ProductID"].Value = ProductID;

if (ChangeProductNameTo.Length > 0) {

try {

SqlCon.Open();
odbcommand.ExecuteNonQuery();
SqlCon.Close();
Message.Text = ChangeProductNameFrom + " <B>HAS BEEN CHANGED TO</B> " + ChangeProductNameTo;

} catch (OleDbException OleDbEx) {

Message.Text = "An Exception Has Occured: " + OleDbEx.Message.ToString();

} finally {

MyDataGrid.EditItemIndex = -1;
BindGrid();

}

} else {

Message.Text = "You must specify a new product name or hit cancel!";

}

}

</script>
<html>
<body>
<form runat="server">
<center>
<asp:Label id="Message" MaintainState="false" ForeColor="#CC3300" Font-Size="11pt" runat="server"/><p>

<ASP:DataGrid
id="MyDataGrid"
runat="server"
Width="800"
CellPadding=3
CellSpacing="0"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="SupplierID"
BorderColor="Tan"
ShowFooter="false"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="Maroon"
AutoGenerateColumns="False"
HeaderStyle-ForeColor="Tan" >

<columns>

<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-VerticalAlign="top" />

<asp:BoundColumn
HeaderText="ID"
ReadOnly="True"
DataField="SupplierID"
ItemStyle-Wrap="false"
ItemStyle-VerticalAlign="top" />

<asp:BoundColumn
HeaderText="ID"
ReadOnly="True"
DataField="CompanyName"
ItemStyle-Wrap="false"
ItemStyle-VerticalAlign="top" />

<asp:TemplateColumn HeaderText="Products" >

<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ProductCount") %> Products
</ItemTemplate>

<EditItemTemplate>

<TABLE Width="100%" CellPadding="0" CellSpacing="0" Border="0">
<TR>
<TD Style="font-size:8" Width="20%">
<b>CHANGE -</b>
</TD>
<TD>
<asp:DropDownList
Runat="server"
Id="edit_Product"
DataSource='<%# GetProducts((int)DataBinder.Eval(Container.DataItem, "SupplierID")) %>'
DataTextField="ProductName"
DataValueField="ProductID"
Width="200" />
</TD>
</TR>
<TR>
<TD Style="font-size:8" Width="20%">
<b>TO - </b>
</TD>
<TD>
<asp:TextBox
id="NewProductName"
runat="Server"
Width="200" />
</TD>
</TR>
</TABLE>

</EditItemTemplate>

</asp:TemplateColumn>

</columns>

</asp:DataGrid>
</center>
</form>
</body>
</html>

ioricat 2002-05-10
  • 打赏
  • 举报
回复
把其中一列转化为模板列后,在aspx文件里修改。
<asp:TemplateColumn SortExpression="hint" HeaderText="学习提示">
<HeaderTemplate>
<FONT face="宋体">学习提示</FONT>
</HeaderTemplate>
<ItemTemplate>
<asp:textbox id=tbhint Font-Size="9pt" text='<%# databinder.eval(container.dataitem,"hint")%>' Runat="server">
</asp:textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:dropdownlist id="edit_hint" BorderWidth="1px" datasource='<%# datasource1%>' datatextfield="textfield" datavaluefield="valuefield" Runat="server" >
</asp:dropdownlist>
</EditItemTemplate>
</asp:TemplateColumn>

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧