ASP.NET动态链接按钮

时间:2009-06-15 09:14:04

标签: asp.net

我正在page_init期间创建一个动态链接,然后绑定onclick到一个链接按钮。单击动态链接可以回发但它没有引发onclick事件(在调试模式下点击一个on click事件)。页面回发并显示空白页面而不触发事件。请允许任何人在下面的代码中告诉我我做错了什么。我使用的是vs2005(ASp.Net 2.0)。请看第一个我动态创建链接按钮的开关案例。

public partial class ChooseRole : System.Web.UI.Page
{
    public Int32 ismId;
    public DataTable UserCredentialsDT;
    public DataTable dsUserDetails = null;
    public String strPage = "";
    protected System.Web.UI.WebControls.LinkButton lnkAdmin;

    //protected System.Web.UI.WebControls.PlaceHolder PlaceHolderRoles;

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session[PresentationConstant.USERCREDENTIALS] != null)
        {
            dsUserDetails = (DataTable)Session[PresentationConstant.USERCREDENTIALS];
            ismId = Convert.ToInt32(dsUserDetails.Rows[0][0].ToString());
        }

        Helper.validateRoleAuthentication(PresentationConstant.PAGE_CHOOSEROLE);
        if (!IsPostBack)
        {            
            if (Session[PresentationConstant.USERCREDENTIALS] != null)
            {
                UserCredentialsDT = (DataTable)Session[PresentationConstant.USERCREDENTIALS];

                //Sorting the roles
                DataView v = UserCredentialsDT.DefaultView;
                v.Sort = "roleName";
                UserCredentialsDT = v.ToTable();

                Int32 ismId = Convert.ToInt32(UserCredentialsDT.Rows[0][0].ToString());

                for (int i = 0; i < UserCredentialsDT.Rows.Count; i++)
                {

                    switch (Convert.ToInt32(UserCredentialsDT.Rows[i]["RoleId"]))
                    {
                        case (int)Helper.UserRole.Administrator:

                            //Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                             //LinkButton link = new LinkButton();
                             //{
                             //    link.Text = "Test< /br>";
                             //    link.ID = "TestButton";
                             //    link.Click += new EventHandler(TestClicked);


                             //}
                             //PlaceHolderRoles.Controls.Add(link);
                            lnkAdmin = new LinkButton();
                            lnkAdmin.EnableViewState = true;
                            lnkAdmin.Text = "Administrator" + "<br />";
                            lnkAdmin.ID = "lnkAdmin";

                            this.lnkAdmin.Click += new EventHandler(this.lnkAdmin_Click);
                            this.PlaceHolderRoles.Controls.Add(lnkAdmin);


                            //lnkAdmin.Visible = true;

                            break;


                        case (int)Helper.UserRole.KeyHolder:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            String strPage;
                            LinkButton lnkSM = new LinkButton();
                            lnkSM.EnableViewState = true;
                            lnkSM.Text = "Manager / Key Holder" + "<br />";
                            lnkSM.ID = "lnkSM" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkSM);
                            lnkSM.Visible = true;
                            strPage = GetStoresForStoreManager_Table(ismId);
                            if (!String.IsNullOrEmpty(strPage))
                            {
                                lnkSM.PostBackUrl = strPage;
                            }
                            break;

                        case (int)Helper.UserRole.DistrictManager:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            LinkButton lnkDM = new LinkButton();
                            lnkDM.EnableViewState = true;
                            lnkDM.Text = "District Manager" + "<br />";
                            lnkDM.ID = "lnkDM" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkDM);
                            lnkDM.Visible = true;
                            lnkDM.PostBackUrl = PresentationConstant.ASPX_DMREPORTS;
                            break;

                        case (int)Helper.UserRole.ARD:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            LinkButton lnkARD = new LinkButton();
                            lnkARD.EnableViewState = true;
                            lnkARD.Text = "ARD" + "<br />";
                            lnkARD.ID = "lnkARD" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkARD);
                            lnkARD.Visible = true;
                            lnkARD.PostBackUrl = PresentationConstant.ASPX_ARDREPORTS;
                            break;

                        case (int)Helper.UserRole.TeamAdvisor:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            LinkButton lnkTeamAdvisor = new LinkButton();
                            lnkTeamAdvisor.EnableViewState = true;
                            lnkTeamAdvisor.Text = "Team Advisor" + "<br />";
                            lnkTeamAdvisor.ID = "lnkTeamAdvisor" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkTeamAdvisor);
                            lnkTeamAdvisor.Visible = true;
                            lnkTeamAdvisor.PostBackUrl = PresentationConstant.ASPX_DMREPORTS;
                            break;

                        case (int)Helper.UserRole.RPC:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            LinkButton lnkRPC = new LinkButton();
                            lnkRPC.EnableViewState = true;
                            lnkRPC.Text = "RPC" + "<br />";
                            lnkRPC.ID = "lnkRPC" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkRPC);
                            lnkRPC.Visible = true;
                            lnkRPC.PostBackUrl = PresentationConstant.ASPX_ARDREPORTS;
                            break;

                        case (int)Helper.UserRole.RegionalManager:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            LinkButton lnkRegionalManager = new LinkButton();
                            lnkRegionalManager.EnableViewState = true;
                            lnkRegionalManager.Text = "Regional Manager" + "<br />";
                            lnkRegionalManager.ID = "lnkRegionalManager" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkRegionalManager);
                            lnkRegionalManager.Visible = true;
                            lnkRegionalManager.PostBackUrl = PresentationConstant.ASPX_RMREPORTS;
                            break;

                        case (int)Helper.UserRole.MerchAdmin:
                            Session[AppConstants.ROLEID] = UserCredentialsDT.Rows[i]["RoleId"];
                            LinkButton lnkMerchAdmin = new LinkButton();
                            lnkMerchAdmin.EnableViewState = true;
                            lnkMerchAdmin.Text = "Merch Admin" + "<br />";
                            lnkMerchAdmin.ID = "lnkMerchAdmin" + i.ToString();
                            PlaceHolderRoles.Controls.Add(lnkMerchAdmin);
                            lnkMerchAdmin.Visible = true;
                            lnkMerchAdmin.PostBackUrl = PresentationConstant.ASPX_MERCHANDISINGADMIN;
                            break;

                        default:
                            ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script type=\"text/javascript\"> alert('Invalid Role.');</script>");
                            break;
                    }

                }
            }
        }
    }



  private String GetStoresForStoreManager_Table(Int32 iStoreManagerId)
    {
        String strDestinationPage = String.Empty;
        String strStoreNo = String.Empty;
        String strStoretypeId = String.Empty;

        String strStoretype = String.Empty;
        DataTable dtStoreDetails = null;

        StoreBL objStoreBL = new StoreBL();
        dtStoreDetails = objStoreBL.GetStoresForStoreManager_Table(iStoreManagerId);

        if (dtStoreDetails.Rows.Count != 0)
        {
            if (dtStoreDetails.Rows.Count > 1)
            {
                strDestinationPage = PresentationConstant.ASPX_CHOOSESTORE;
            }
            else
            {
                strStoreNo = dtStoreDetails.Rows[0][AppConstants.STORENO].ToString();
                strStoretypeId = dtStoreDetails.Rows[0][AppConstants.ID].ToString();
                if (dtStoreDetails.Rows[0][AppConstants.ISRETAIL].ToString().ToLower() == "true")
                {
                    strStoretype = DBConstants.RETAIL;
                }
                else
                {
                    strStoretype = DBConstants.SandR;
                }
                Session.Add(AppConstants.STORENO, strStoreNo);
                Session.Add(AppConstants.STORETYPEID, strStoretypeId);
                Session.Add(AppConstants.STORETYPE, strStoretype);
                strDestinationPage = PresentationConstant.ASPX_STOREMAIN;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script type=\"text/javascript\"> alert('You are not associated with any store.');window.location = 'SprintLogin.aspx';</script>");
        }
        return strDestinationPage;

    }
    protected void lnkAdmin_Click(object sender, System.EventArgs e)
    {
        Response.Redirect(PresentationConstant.ASPX_MERCHANDISINGADMIN);
    }

3 个答案:

答案 0 :(得分:1)

该行

if (!IsPostBack)

将导致您的页面不在回发中创建动态按钮,因此ASP.Net框架将找不到引发该事件的控件。

我想知道为什么你没有得到ViewState异常。

答案 1 :(得分:0)

我认为你应该create dynamic controls in the PreInit event handler。但是在这种情况下,devio的答案看起来就像是你问题的真正原因。

答案 2 :(得分:0)

我认为你最好只在你的页面上设置链接按钮并根据你所处的情况设置文本/属性。完全保存所有动态控制麻烦。

编辑:

抱歉,只是看到了你的foreach - 也许是转发器中的链接。