开发者

How to use user control multiple times on single page(Asp.net C#)

I'm making one user control. This user control is for to display advertisment from database. Just we have to pass the place id and it will fetch the record from database and display on specific page. It's working perfectly but when im draging this user control multiple time on same page then it doesn't work perfectly. Means im passing different different place id for each usercontrol but both usercontrol takes only one place id and display same content in both usercontrol

Here is the code im using

public partial class Advertisment : System.Web.UI.UserControl
{

    static int _Advertismentid;
    #region Get Set Property
    public int Placeid
    {
        get
        {
            return _Advertismentid;
        }
        set
        {
            _Advertismentid = value;
        }
    }
    #endregion


    #region Load Advertisment Function
    public void FnLoadAdvetsiment()
    {

        DataTable dt = new cls_Advertisements().FnGetAdvertsimentContent(Convert.ToInt32(Placeid));
        int i = 0;
        DataView dv = new DataView();
        if (dt.Rows.Count > Convert.ToInt32(Ses开发者_如何学编程sion["rowno"]))
        {
            i = Convert.ToInt32(Session["rowno"]);
            ltrAdvertisment.Text = (dt.Rows[i]["Content"].ToString());
            AdTimer.Interval = Convert.ToInt32(dt.Rows[i]["Timer"].ToString());
            Session["rowno"] = Convert.ToInt32(Session["rowno"]) + 1;
        }
        else
        {
            Session["rowno"] = i = 0;
            ltrAdvertisment.Text = dt.Rows[i]["Content"].ToString();
            AdTimer.Interval = Convert.ToInt32(dt.Rows[i]["Timer"].ToString());
        }

    }


    #endregion



    #region Page Load Event
    protected void Page_Load(object sender, EventArgs e)
    {

            if (!IsPostBack)
            {
                FnLoadAdvetsiment();
                if (Session["rowno"] == null)
                {
                    Session["rowno"] = 0;
                }

            }




    }
    #endregion

    #region Timer Tick Event
    protected void Timer1_Tick(object sender, EventArgs e)
    {

        FnLoadAdvetsiment();

    }
    #endregion
}


I assume your problem is that both ads are displaying the same content.

Its likely this is because you are using the same sessions variables to store the data in. Try prefixing the session key with the id of the control or something else unique or even better try to not sure session at all if you can.


When you declare a variable as "static" then only a single instance of the variable is referenced across all different instances of your control class.

Effectively, the last value you set the "_Advertismentid" variable to will be the same value for all controls.

Declare your variable as;

int _Advertismentid;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜