2011-01-31 63 views
0

我有一个名为Links的表。 两个存储过程称为sp_InsertLinks,sp_GetLinks。Web部件按钮单击

我有一个简单的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; 
     } 
    } 
} 

谢谢

哈日

+0

偏离主题,但在“sp_”前加存储过程被认为是一种不好的做法:http://stackoverflow.com/questions/871612/whats-the-best-practice-of-naming-stored-procedure-for- T-SQL – 2011-01-31 16:38:47

回答

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) { 

    } 
} 

如果你做这种方式,您的活动会更多的,往往不是正确火。

0

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

createchildcontrol()中的connectionmethod之前

希望这能起作用。