WebPart-Button单击

时间:2011-01-31 12:03:10

标签: sharepoint-2007

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

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

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

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

当我提交此页面时,页面将加载正常sharepoint生命周期的事件序列。

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

有人可以看看这个吗?

守则是:

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

谢谢

2 个答案:

答案 0 :(得分:0)

我几乎总是建议在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) {

    }
}

如果你这样做,你的事件往往会正确地开火。

答案 1 :(得分:0)

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

在createchildcontrol()

中的connectionmethod之前 希望这有效。