WebPartManager与SQL

 

WebPartManager与SQL 

原文链接:http://blog.sina.com.cn/s/blog_50f37f0b01011s4r.html

 

今天想学习WebPartManager,在页面上添加了空间之后,运行时发生错误,提示:“webpartmanager SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错 ”,解决方法如下:

1.在web.config文件中添加如下代码:


   

   

    providerName="System.Data.SqlClient" />
 

运行后提示:sa登录失败,经查是由于没有aspnetdb数据库。

2.运行目录C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727下的aspnet_regsql.exe文件,采用默认设置,即安装了aspnetdb数据库,之后运行程序,大功告成。

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class DisplayModeMenu : System.Web.UI.UserControl
{
    WebPartManager webpartmanager;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Page.InitComplete+=new EventHandler(InitComplete);
    }

    public void InitComplete(object sender, EventArgs e)
    {
        webpartmanager = WebPartManager.GetCurrentWebPartManager(Page);
        string browseModeName = WebPartManager.BrowseDisplayMode.Name;
        foreach (WebPartDisplayMode mode in webpartmanager.SupportedDisplayModes)
        {
            string modename = mode.Name;
            if (mode.IsEnabled(webpartmanager))
            {
                ListItem listitem = new ListItem(modename, modename);
                DropDownList1.Items.Add(listitem);
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedmode = DropDownList1.SelectedValue;
        WebPartDisplayMode mode = webpartmanager.SupportedDisplayModes[selectedmode];
        if (mode != null)
        {
            webpartmanager.DisplayMode = mode;
        }
    }

    public void Page_PreRender(object sender, EventArgs e)
    {
        ListItemCollection items = DropDownList1.Items;
        int selectedIndex = items.IndexOf(items.FindByText(webpartmanager.DisplayMode.Name));
        DropDownList1.SelectedIndex = selectedIndex;
    }
}

你可能感兴趣的:(WebPart)