你好,所有的专业人士,
我正在使用 Visual Studio 2010,并在 c# asp.net 中编程
我有一个用户控件,我正在动态加载到我的 aspx 页面......
这是代码
protected void Page_Load(object sender, EventArgs e)
{
Utils u = new Utils();
QueryBuilder newQRY = new QueryBuilder();
string Command = "SELECT Cast.Submit_Date, Cast.Cast_ID, Cast.Game_Frame, Cast.Race_1, Cast.Race_2, Cast.Language, Cast.Map, Cast.Serie_Name, Cast.Cast_URL, Cast.Like_Amount, Caster.Caster_LOGO, Caster.Caster_Name, Player.Player_Name, Player_1.Player_Name AS Expr1 FROM Cast INNER JOIN Caster ON Cast.Caster = Caster.Caster_ID INNER JOIN Player ON Cast.Player1 = Player.Player_ID INNER JOIN Player AS Player_1 ON Cast.Player2 = Player_1.Player_ID ORDER BY Cast.Submit_date";
SqlConnection connString = u.connect("NewConnectionString");
SqlDataAdapter adpWatchLaterSession = new SqlDataAdapter(Command, connString);
DataSet dscasts = new DataSet();
adpWatchLaterSession.Fill(dscasts);
DataTable dt = new DataTable();
string watchLaterCastsQRY = newQRY.buildCastIDrelatedtoUserQuary();
adpWatchLaterSession = new SqlDataAdapter(watchLaterCastsQRY, connString);
adpWatchLaterSession.Fill(dt);
for (int i = 0; i < dscasts.Tables[0].Rows.Count; i++)
{
SearchBulletV2 sb1 = (SearchBulletV2)Page.LoadControl("SearchBulletV2.ascx");
sb1.ID = "sb" + dscasts.Tables[0].Rows[i]["Cast_ID"].ToString();
CheckBox cbwatchlater = new CheckBox();
cbwatchlater = sb1.FindControl("cbWatchLater") as CheckBox;
cbwatchlater.ID = "cbWatchLater" + dscasts.Tables[0].Rows[i]["Cast_ID"].ToString();
cbwatchlater.AutoPostBack = true;
for (int j = 0; j < dt.Rows.Count; j++)
{
if (String.Compare(dt.Rows[j][0].ToString(), dscasts.Tables[0].Rows[i]["Cast_ID"].ToString()) == 0)
{
cbwatchlater.Checked = true;
break;
}
}
cbwatchlater.CheckedChanged += new EventHandler(cbwatchlater_CheckedChanged);
AjaxControlToolkit.ToggleButtonExtender toggle = new AjaxControlToolkit.ToggleButtonExtender();
toggle = sb1.FindControl("cbWatchLater_ToggleButtonExtender") as AjaxControlToolkit.ToggleButtonExtender;
toggle.TargetControlID = "cbWatchLater" + dscasts.Tables[0].Rows[i]["Cast_ID"].ToString();
Image race1img = new Image();
race1img = sb1.FindControl("ImageRace1") as Image;
race1img.ImageUrl = dscasts.Tables[0].Rows[i]["Race_1"].ToString();
Image race2img = new Image();
race1img = sb1.FindControl("ImageRace2") as Image;
race1img.ImageUrl = dscasts.Tables[0].Rows[i]["Race_2"].ToString();
Image casterimg = new Image();
race1img = sb1.FindControl("CasterLOGOIMG") as Image;
race1img.ImageUrl = dscasts.Tables[0].Rows[i]["Caster_LOGO"].ToString();
Label lb = new Label();
lb = sb1.FindControl("PlayerName1") as Label;
lb.Text = dscasts.Tables[0].Rows[i]["Player_Name"].ToString();
lb = sb1.FindControl("PlayerName2") as Label;
lb.Text = dscasts.Tables[0].Rows[i]["Expr1"].ToString();
lb = sb1.FindControl("CasterName") as Label;
lb.Text = dscasts.Tables[0].Rows[i]["Caster_Name"].ToString();
lb = sb1.FindControl("Map") as Label;
lb.Text = dscasts.Tables[0].Rows[i]["Map"].ToString();
lb = sb1.FindControl("GameFrame") as Label;
lb.Text = dscasts.Tables[0].Rows[i]["Game_Frame"].ToString();
lb = sb1.FindControl("LikeAmount") as Label;
lb.Text = dscasts.Tables[0].Rows[i]["Like_Amount"].ToString();
lb = sb1.FindControl("lblSubDate") as Label;
string subdate = dscasts.Tables[0].Rows[i]["Submit_date"].ToString();
lb.Text = subdate.Remove(10, 9);
HyperLink link = new HyperLink();
link = sb1.FindControl("PlayHyperLink") as HyperLink;
link.NavigateUrl = "ViewVideo.aspx?castername=" + dscasts.Tables[0].Rows[i]["Caster_Name"].ToString() + "&castid=" + dscasts.Tables[0].Rows[i]["Cast_ID"].ToString() + "&player1name=" + dscasts.Tables[0].Rows[i]["Player_Name"].ToString() + "&player2name=" + dscasts.Tables[0].Rows[i]["Expr1"].ToString() + "&race1=" + dscasts.Tables[0].Rows[i]["Race_1"].ToString() + "&race2=" + dscasts.Tables[0].Rows[i]["Race_2"].ToString() + "&gameframe=" + dscasts.Tables[0].Rows[i]["Game_Frame"].ToString() + "&serie=" + dscasts.Tables[0].Rows[i]["Serie_Name"].ToString() + "&map=" + dscasts.Tables[0].Rows[i]["Map"].ToString() + "&like=" + dscasts.Tables[0].Rows[i]["Like_Amount"].ToString() + "&casturl=" + dscasts.Tables[0].Rows[i]["Cast_URL"].ToString() + "?wmode=transparent" + "&casterlogo=" + dscasts.Tables[0].Rows[i]["Caster_LOGO"].ToString();
HiddenField hd = new HiddenField();
hd = sb1.FindControl("HiddenField1") as HiddenField;
hd.Value = dscasts.Tables[0].Rows[i]["Cast_ID"].ToString();
UserControlPlaceHolder1.Controls.Add(sb1);
}
}
如您所见,这是很多代码(至少对我而言)。我想做的就是把它全部带到某个类,在那里做,然后在 page_Load 上只放一个返回控制的方法......
我一直在想这样的事情......
for (int i = 0; i < dscasts.Tables[0].Rows.Count; i++)
{
SearchBulletV2 sb1 = (SearchBulletV2)Page.LoadControl("SearchBulletV2.ascx");
tmpsb = someclass.initCtrlProperties(sb1);
UserControlPlaceHolder1.Controls.Add(tmpsb);
}
}
因此,如果我有一些更改,我将不必浏览我所有的 aspx 页面并手动更改它。
我希望你明白我的想法:)
多谢,
埃利佩尔佐夫