asp.net 自定义控件数据绑定的问题

liu_binq63 2008-07-15 10:22:12
最近写了一个自定义控件,功能是根据设置了的type属性,来显示相应的内容,控件代码如下:
WebUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="5">
<asp:ListItem Selected="True">1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True">1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="5">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:CheckBoxList>


WebUserControl.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
[Serializable()]
public partial class WebUserControl : System.Web.UI.UserControl
{
private int _Type = 0;

public int Type
{
get { return _Type; }
set {

_Type = value;
SetVisible();
}
}

protected void Page_Load(object sender, EventArgs e)
{
SetVisible();
}

void SetVisible()
{
switch (_Type)
{
case 0:
RadioButtonList1.Visible = true;
DropDownList1.Visible = false;
CheckBoxList1.Visible = false;
break;
case 1:
RadioButtonList1.Visible = false;
DropDownList1.Visible = true;
CheckBoxList1.Visible = false;
break;

case 2:
RadioButtonList1.Visible = false;
DropDownList1.Visible = false;
CheckBoxList1.Visible = true;
break;
}

}
}



使用这个页面的测试页面:
Default2.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Type") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<uc1:WebUserControl id="WebUserControl1" Type='<%# Convert.ToInt32(Eval("Type")) %>' runat="server">
</uc1:WebUserControl>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>



Default2.aspx.cs


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Type") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<uc1:WebUserControl id="WebUserControl1" Type='<%# Convert.ToInt32(Eval("Type")) %>' runat="server">
</uc1:WebUserControl>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>


Default2.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
DataTable Dt ;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{


} Dt = new DataTable();
Dt.Columns.Add(new DataColumn("ID"));
Dt.Columns.Add(new DataColumn("name"));
Dt.Columns.Add(new DataColumn("type"));
DataRow DR;
DR = Dt.NewRow();
DR["ID"] = 1;
DR["name"] = "aa";
DR["Type"] = 1;
Dt.Rows.Add(DR);

DR = Dt.NewRow();
DR["ID"] = 2;
DR["name"] = "bb";
DR["Type"] = 2;
Dt.Rows.Add(DR);
DR = Dt.NewRow();
DR["ID"] = 3;
DR["name"] = "cc";
DR["Type"] = 0;
Dt.Rows.Add(DR);
GridView1.DataSource = Dt;
GridView1.DataBind();


}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
WebUserControl WUC;
GridView1.SelectedIndex = i;
WUC = (WebUserControl)GridView1.SelectedRow.FindControl("WebUserControl1");
Response.Write(WUC.Type);
}
}
}


现在的问题是,单击提交按钮后,那些单选等控件的选中状态都没有了,怎么才能把这些状态保存下来?或者说得到这些选中状态?
...全文
99 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
万小萌 2008-07-15
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Dt = new DataTable();
Dt.Columns.Add(new DataColumn("ID"));
Dt.Columns.Add(new DataColumn("name"));
Dt.Columns.Add(new DataColumn("type"));
DataRow DR;
DR = Dt.NewRow();
DR["ID"] = 1;
DR["name"] = "aa";
DR["Type"] = 1;
Dt.Rows.Add(DR);

DR = Dt.NewRow();
DR["ID"] = 2;
DR["name"] = "bb";
DR["Type"] = 2;
Dt.Rows.Add(DR);
DR = Dt.NewRow();
DR["ID"] = 3;
DR["name"] = "cc";
DR["Type"] = 0;
Dt.Rows.Add(DR);
GridView1.DataSource = Dt;
GridView1.DataBind();
}

}

zlkingdom 2008-07-15
  • 打赏
  • 举报
回复
应该是不会点个按钮就将其他控件的状态清空的
你把绑定数据的代码写到if(!Page.IsPostBack)下面
写在外面的话点一次按钮就会重新执行一次绑定
liu_binq63 2008-07-15
  • 打赏
  • 举报
回复
谁能帮帮我:(
liu_binq63 2008-07-15
  • 打赏
  • 举报
回复
问题解决了分数散了吧。
liu_binq63 2008-07-15
  • 打赏
  • 举报
回复
fly_goldfish 的方法不管用,单击那个button后的页面就什么都不显示了:(

110,617

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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