Reputation: 21
Hello to all the pros out there,
I am using Visual studio 2010, and programming in c# asp.net
I have a UserControl that i'm loading dynammically to my aspx page...
here is the code
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);
}
}
as you can see, it's a lot of code (for me atleast). What i wanna do is to take it all to some class, do it there and on a page_Load put only a method that will return the control...
i've been thinking something like this.....
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);
}
}
so if i have some changes, i won't have to go through all my aspx pages and change it manually.
i hope you got my idea :)
thanks alot,
Elli pertzov
Upvotes: 0
Views: 156
Reputation: 68440
Your idea is correct, I'd move it to a separated method in the same page instead of creating a new class.
I'll add something else
Move this code to a separated data access class and dispose resources
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);
SqlConnection
, SqlDataAdapter
, SqlCommand
and DataSet
implement IDisposable
you should call dispose method.
You could do it automatically if you use the instruction using
. For example:
using(var conn = new SqlConnection("connectionstring"))
{
....
}
Upvotes: 1