在单独的类中设置UserControl属性

时间:2012-06-07 13:42:40

标签: c# asp.net user-controls

欢迎所有专业人士,

我正在使用Visual Studio 2010,并使用c#asp.net编程

我有一个UserControl,我正在动态加载到我的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); 

        } 

    }

正如你所看到的,这是很多代码(至少对我而言)。我想做的是把它全部带到某个类,在那里和页面上做.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页面并手动更改。

我希望你有我的想法:)

非常感谢,

Elli pertzov

1 个答案:

答案 0 :(得分:1)

您的想法是正确的,我会将其移至同一页面中的分离方法,而不是创建新类。

我会添加别的东西

将此代码移至分离的数据访问类并处置资源

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); 

SqlConnectionSqlDataAdapterSqlCommandDataSet实施IDisposable您应该调用dispose方法。

如果您使用using指令,则可以自动执行此操作。例如:

using(var conn = new SqlConnection("connectionstring"))
{
   ....
}