请帮助我的人,我在asp.net(C#代码)中有一个gridview,其中两列名为“Type”,另一列名为“Save”。在“Type”列中,一些数据随机定量和定性。所以如果数据在“类型”列中是定量的,则同一行中“保存”列中的对应单元格应为 DropdownList,如果是定性列,则同一行中的“保存”列中对应的单元格将为 TextBox。
提前致谢
在“保存”列中,创建一个模板字段,并放置 Textbox 和 Dropdown,然后当每行数据绑定时,运行一个函数来检查 Type 列中的数据类型并隐藏(对于该行)保存列中不需要的元素:
下面是代码的样子:
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
bool isQuantitative = ((CheckBox)e.Row.FindControl("cb1")).Checked;
if (isQuantitative)
{
((textBox)e.Row.FindControl("myTextboxID")).Visible = true;
((DropDownList)e.Row.FindControl("myDdlistID")).Visible = false;
}
else
{
((textBox)e.Row.FindControl("myTextboxID")).Visible = false;
((DropDownList)e.Row.FindControl("myDdlistID")).Visible = true;
}
}
}
上例中用于检查定量/定性数据的控件是一个复选框,将其替换为您的控件或逻辑。
然后要将 gridview 链接到此方法,请将此属性添加到 gridview 声明中:
OnRowDataBound="myGridView_RowDataBound"
我正在为您的程序的数据源寻求答复。无论如何,我已经为你创建了演示。这是您的完整答案。
这是您的页面:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="EnableAndDisableControlsGridviewWebApp._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<div>
<asp:GridView ID="GridView1" AutoGenerateColumns="false"
runat="server" onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblId" Style="width: 100px;" runat="server"
Text='<%# Eval("Id")%>' Visible="false"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblId" Style="width: 100px;" runat="server"
Text='<%# Eval("Id")%>' Visible="false"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="lblCustomType" Style="width: 100px;" runat="server"
Text='<%# Eval("CustomType")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblCustomType" Style="width: 100px;" runat="server"
Text='<%# Eval("CustomType")%>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:TextBox ID="txtSave" Style="width: 100px;" runat="server"></asp:TextBox>
<asp:DropDownList ID="DrpSave" Style="width: 100px;" runat="server"></asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSave" Style="width: 100px;" runat="server"></asp:TextBox>
<asp:DropDownList ID="DrpSave" Style="width: 100px;" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
你后面的代码是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace EnableAndDisableControlsGridviewWebApp
{
public partial class _Default : System.Web.UI.Page
{
DataTable aTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
aTable.Columns.Add("Id", typeof(int));
aTable.Columns.Add("CustomType", typeof(string));
aTable.Columns.Add("CustomSave", typeof(string));
DataRow dr1 = aTable.NewRow();
dr1["Id"] = 1;
dr1["CustomType"] = "qualitative randomly";
dr1["CustomSave"] = "DropdownList";
aTable.Rows.Add(dr1);
DataRow dr2 = aTable.NewRow();
dr2["Id"] = 2;
dr2["CustomType"] = "quantitative";
dr2["CustomSave"] = "TextBox";
aTable.Rows.Add(dr2);
DataRow dr3 = aTable.NewRow();
dr3["Id"] = 3;
dr3["CustomType"] = "qualitative randomly";
dr3["CustomSave"] = "DropdownList";
aTable.Rows.Add(dr3);
DataRow dr4 = aTable.NewRow();
dr4["Id"] = 4;
dr4["CustomType"] = "quantitative";
dr4["CustomSave"] = "TextBox";
aTable.Rows.Add(dr4);
GridView1.DataSource = aTable;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblIdentifier = (Label)e.Row.FindControl("lblCustomType");
string val = lblIdentifier.Text;
if (val == "quantitative")
{
((TextBox)e.Row.FindControl("txtSave")).Visible = true;
((DropDownList)e.Row.FindControl("DrpSave")).Visible = false;
}
else
{
((TextBox)e.Row.FindControl("txtSave")).Visible = false;
((DropDownList)e.Row.FindControl("DrpSave")).Visible = true;
}
}
}
}
}