繁体   English   中英

在单独的类中设置UserControl属性

[英]Setting UserControl properties in separate class

大家好,大家好

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

我有一个要动态加载到aspx页面的UserControl ...

这是代码

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页面并手动进行更改。

我希望你有我的主意:)

非常感谢,

埃利·佩尔佐夫(Elli Pertzov)

您的想法是正确的,我将其移到同一页中的单独方法中,而不是创建一个新类。

我再加点东西

将此代码移到单独的数据访问类并处理资源

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"))
{
   ....
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM