【问题标题】:WebPart-Button ClickWebPart-按钮单击
【发布时间】:2011-01-31 12:03:10
【问题描述】:

我有一个名为 Links 的表。 两个存储过程分别称为 sp_InsertLinks、sp_GetLinks。

我有一个简单的 webpart,它接受两个参数并将其添加到 SQL 表调用链接。

在第一个界面中,它显示来自数据库的值列表和添加列表的按钮。

当我点击链接时,它会显示下一个界面,我可以在其中添加链接名称的 txtbox 和链接 URL 的文本框。

当我提交此页面时,该页面正在按正常共享点生命周期的事件顺序加载。

而且我无法将新链接添加到页面中,因为按钮单击方法永远不会被触发。

有人可以看看这个吗?

代码是:

  using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Text ;
using System.Data ;
using System.Data.SqlClient;
using System.Drawing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace ContextMenuOptionsUsingJQuery
{
    [Guid("7a3a52d4-9ad6-44b2-b96f-852da1a95371")]
    public class ContextMenuOptionsUsingJQuery : System.Web.UI.WebControls.WebParts.WebPart
    {

        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader dr;
        string Con_string = string.Empty;
        Button btnAddLink;
        Button btnAddNewLink;
        StringBuilder outputDisplay;
        TextBox txtLink;
        TextBox txtLinkUrl;
        Label lblDisplay = new Label();

        public ContextMenuOptionsUsingJQuery()
        {

        }



         protected override void CreateChildControls()
        {
            try
            {

                // Getting the Connection 

                ConnectionMethod();

                // Calling the Appropraite Method or stored Procedures

                RefreshData();



                // Adding a New Link though the button

                    btnAddLink = new Button();
                    btnAddLink.Text = "Add Link";
                    btnAddLink.Click += new EventHandler(btn_AddLink);

                    //New item

                    Controls.Add(btnAddLink);



            }
            catch (Exception e)
            {
                Label l = new Label();
                l.Text = e.StackTrace;
                Controls.Add(l);
            }         
        }



        // Button Add Link
        private void btn_AddLink(Object sender, EventArgs e)
        {
            Controls.Clear();
            btnAddNewLink = new Button();
            txtLink = new TextBox();
            txtLinkUrl = new TextBox();
            Controls.Add(txtLink);
            Controls.Add(txtLinkUrl);                
            btnAddNewLink.Text = "ADD NEW Link";
            btnAddNewLink.Click += new EventHandler(btnAddNewLink_Click);
            Controls.Add(btnAddNewLink);

        }
        private void btnAddNewLink_Click(Object sender, EventArgs e)
        {
            int i;
            try
            {
                ConnectionMethod();
                cmd.CommandText = "sp_InsertLinks";
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter paramLinkName = new SqlParameter("@LinkName", SqlDbType.VarChar, 50);
                SqlParameter paramLinkUrl = new SqlParameter("@LinkUrl", SqlDbType.VarChar, 50);
                paramLinkName.Direction = ParameterDirection.Input;
                paramLinkUrl.Direction = ParameterDirection.Input;
                paramLinkName.Value = txtLink.Text.ToString();
                paramLinkUrl.Value = txtLinkUrl.Text.ToString();
                cmd.Parameters.Add(paramLinkUrl);
                cmd.Parameters.Add(paramLinkName);
                i = cmd.ExecuteNonQuery();
                con.Close();
                ConnectionMethod();
                RefreshData();
            }
            catch (Exception exp)
            {
                Label l = new Label();
                l.Text = exp.StackTrace;
                Controls.Add(l);
            }
            finally
            {
                con.Close();
            }         

        }
        private void RefreshData()
        {
            cmd.CommandText = "sp_GetLinks";
            cmd.CommandType = CommandType.StoredProcedure;
            dr = cmd.ExecuteReader();

            outputDisplay = new System.Text.StringBuilder();
            outputDisplay.AppendLine("<br/>");

            // Fetching the Data from the Datareader object

            while (dr.Read())
            {
                outputDisplay.AppendLine("<a href=" + dr[0].ToString() + ">" + dr[1] + "</a>" + "<br/><br/>");
            }
            con.Close();
            outputDisplay.AppendLine("<br/> <br/>");
            lblDisplay.Text = outputDisplay.ToString();
            Controls.Add(lblDisplay);

        }


        // Method to get the Connection

        public void ConnectionMethod()
        {
            con = new SqlConnection();
            cmd = new SqlCommand();
            Con_string = "Data Source=servername;Initial Catalog=HariVMTest;Integrated Security=True";
            con.ConnectionString = Con_string;
            con.Open();
            cmd.Connection = con;
        }
    }
}

谢谢

哈里

【问题讨论】:

标签: sharepoint-2007


【解决方案1】:

我几乎总是建议在 CreateChildControls() 中创建所有控件

那么您应该使用Visible 属性来根据需要显示和隐藏控件。

然后代码将如下所示:

public class ContextMenuOptionsUsingJQuery : System.Web.UI.WebControls.WebParts.WebPart {

    Button btnAddLink;
    Button btnAddNewLink;

    protected override void CreateChildControls() {
        btnAddLink = new Button();
        btnAddLink.Text = "Add Link";
        btnAddLink.Click += new EventHandler(btn_AddLink);
        Controls.Add(btnAddLink);    

        btnAddNewLink.Text = "ADD NEW Link";
        btnAddNewLink.Click += new EventHandler(btnAddNewLink_Click);
        btnAddNewLink.Visible = false;
        Controls.Add(btnAddNewLink);
    }

    private void btn_AddLink(Object sender, EventArgs e) {
        btnAddLink.Visible = false;
    }

    private void btnAddNewLink_Click(Object sender, EventArgs e) {

    }
}

如果你这样做,你的事件通常会正确触发。

【讨论】:

    【解决方案2】:

    我认为您只需要添加: // 通过按钮添加新链接 btnAddLink = 新按钮(); btnAddLink.Text = "添加链接"; btnAddLink.Click += new EventHandler(btn_AddLink);

    createchildcontrol() 中的连接方法之前

    希望这行得通。

    【讨论】:

      猜你喜欢
      • 2018-05-16
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      相关资源
      最近更新 更多