熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.net中gridview疑難解答

2022-06-13   來源: .NET編程 

   在網頁中制作了一個gridview其中一列是按鈕列(此列已有click事件)

  現在需要實現如下的功能)單擊此列某個cells後能在此cells右下方出現關於點擊內容的詳細說明窗口(可以是窗口也可以是其他))當鼠標再點擊詳細說明窗口以外的區域時這個說明窗口自動消失

  可直接用ToolTip屬性代碼如下

    for (int i = ; i <= GridViewRowsCount ; i++)
{
DataRowView mydrv;
string gIntro;
if (GridViewPageIndex == )
{
mydrv = mydsTables[飛狐工作室]DefaultView;//表名
gIntro = ConvertToString(mydrv[家庭住址]);//所要處理的字段
GridViewRowsCells[]Text = SubStr(gIntro );
}
else
{
mydrv = mydsTables[飛狐工作室]DefaultView[i + ( * GridViewPageIndex)];
gIntro = ConvertToString(mydrv[家庭住址]);
GridViewRowsCells[]Text = SubStr(gIntro );
GridViewRowsCells[] ToolTip=gIntro
}
}

  調用的方法
    
    

    public string SubStr(string sString int nLeng)
{
if (sStringLength <= nLeng)
{
return sString;
}
string sNewStr = sStringSubstring( nLeng);
sNewStr = sNewStr + ;
return sNewStr;
}


    
    GridView中的超鏈接問題

  GridView中有一個字段是超鏈接根據該gridview中的另一個字段中內容的不同超鏈接指向不同的網頁比如新浪網易等這樣的超鏈接是在gridview中編輯出來然後在cs文件中寫if else語句進行判斷嗎?

  代碼如下

    HTML code
<%@ Page Language=C# AutoEventWireup=true EnableEventValidation=false CodeFile=GridViewaspxcs Inherits=GridView %>

<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

<html xmlns= >
<head runat=server>
<title>無標題頁</title>
</head>
<body >
<form id=form runat=server >

<div>
<asp:GridView ID=GridView runat=server OnRowDataBound=GridView_RowDataBound >
<Columns>
<asp:BoundField DataField=學生班級 />
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink id=hl runat=server NavigateUrl=<%# getUrl(Eval(學生班級)ToString()) %> Text=<%# getName(Eval(學生班級)ToString()) %>></asp:HyperLink>
<asp:LinkButton ID=lbtn runat=server ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>


</form>
</body>
</html>
C# code

using System;
using SystemData;
using SystemConfiguration;
using SystemCollections;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;

using SystemDataSqlClient;

public partial class GridView : SystemWebUIPage
{

protected ICollection CreateDataSource()
{

SystemDataDataTable dt = new SystemDataDataTable();
SystemDataDataRow dr;
dtColumnsAdd(new SystemDataDataColumn(學生班級 typeof(SystemString)));
dtColumnsAdd(new SystemDataDataColumn(學生姓名 typeof(SystemString)));
dtColumnsAdd(new SystemDataDataColumn(語文 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(數學 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(英語 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(計算機 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(布爾 typeof(SystemBoolean)));

for (int i = ; i < ; i++)
{
SystemRandom rd = new SystemRandom(EnvironmentTickCount * i); ;
dr = dtNewRow();
dr[] = 班級 + iToString();
dr[] = 學生 + iToString();
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = i % == ? true : false;
dtRowsAdd(dr);
}
SystemDataDataView dv = new SystemDataDataView(dt);

}  

  代碼接上
    

    protected string getUrl(string tempStr)
{
string url = ;
switch (tempStr)
{
case 班級:
url = ;
break;
case 班級:
url = ;
break;
case 班級:
url = ;
break;
case 班級:
url = ;
break;
default:
url = defaultaspx;
break;
}
return url;
}
protected string getName(string tempStr)
{
string name = ;
switch (tempStr)
{
case 班級:
name = 新浪;
break;
case 班級:
name = 百度;
break;
case 班級:
name = 谷歌;
break;
case 班級:
name = Tom;
break;
default:
name = defaultaspx;
break;
}
return name;
}


protected void Page_Load(object sender EventArgs e)
{


GridViewDataSource = CreateDataSource();
GridViewDataBind();

}



protected void GridView_RowDataBound(object sender GridViewRowEventArgs e)
{
if (eRowRowType == DataControlRowTypeDataRow)
{

LinkButton lbtn = eRowFindControl(lbtn) as LinkButton;
if (lbtn != null)
{
string tempStr = eRowCells[]Text;
switch (tempStr)
{
case 班級:
lbtnPostBackUrl = ;
lbtnText = 新浪;
break;
case 班級:
lbtnPostBackUrl = ;
lbtnText = 百度;
break;
case 班級:
lbtnPostBackUrl = ;
lbtnText = 谷歌;
break;
case 班級:
lbtnText = TOM;
lbtnPostBackUrl = ;
break;
default:
lbtnText = defaultaspx;
lbtnPostBackUrl = defaultaspx;
break;
}
}

}


}
}    

  GridView 是否有滾動條的功能?

  開發的時候用到了GridView控件由於查詢的數據列較多要顯示很寬不知道在aspnet中有沒有滾動條的功能另外能不能限定GridView的寬度不能讓它自由的伸縮

  可采用如下的樣式

    margin:auto;
width:%;
overflow:scroll;
_height:expression(thisscrollHeight>?px:auto);


From:http://tw.wingwit.com/Article/program/net/201311/13766.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.