請幫我一個人,我有一個gridview在asp.net(C#代碼)有兩列命名爲'類型',另一個'保存'。'類型'列一些數據就像定性和定性如果數據在「類型」列中定量,則同一行中的「保存」列中的對應單元應該是DropdownList,如果它是定性的,那麼相同行中的對應單元即「保存」將是文本框。Gridview操作
由於提前
請幫我一個人,我有一個gridview在asp.net(C#代碼)有兩列命名爲'類型',另一個'保存'。'類型'列一些數據就像定性和定性如果數據在「類型」列中定量,則同一行中的「保存」列中的對應單元應該是DropdownList,如果它是定性的,那麼相同行中的對應單元即「保存」將是文本框。Gridview操作
由於提前
在列「保存」,創建一個模板字段,並把兩個文本框,下拉列表,然後將各行的數據綁定,你運行一個函數來檢查你有什麼類型的數據類型列和隱藏(該行)在保存列不需要的元素:
這裏的代碼可能是什麼樣子:
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;
}
}
}
}
}