你可在超链接中所链接的页面中判断传过来的参数值,如需要可有Response.Redirec("要去的页面").
解决方案 »
- 了解lucene.net中用KTDictSeg分词的朋友进来下
- 更新数据后,重新load这个页面,没有读出最新数据???。。。
- pageCount获取不到值。急急急急急急急急急急急急急急急急急
- 如何让Cache依赖来一个目录。就是说,目录下任何一个文件有修改,缓存就更新。
- 请教hyperlink控件的NavigateUrl方法!!
- 坐标问题
- 如果保存上次打开程序时设置的工作路径?
- 求“办公系统”下载!!!!
- 菜鸟问题:面向对象的语言怎么说?ASP ? ASP.NET ?C# ??
- 谁会web service的调用?????(详细的)
- XML和DataGrid结合时怎么不能显示中文
- ★★★如何把DataGrid中的内容存到一个文本文件并当成附件发给指定的EM???
<asp:TemplateColumn HeaderText="政府公告标题" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:HyperLink id=Title runat="server" Target=_blank text='<%# DataBinder.Eval(Container, "DataItem.Title") %>' NavigateUrl='<%# "GovAfficheShow.aspx?NodeCode="+DataBinder.Eval(Container, "DataItem.NodeCode") %>' style="width:200px;" />
</ItemTemplate>
</asp:TemplateColumn>
然后在DataGrid的ItemCreated事件中这样写
HyperLink h=e.Item.FindControl("HyperLink1");
h.NavigateUrl="你需要的页面";
e.target="_blank";
假定 你的datagrid的名字叫 dataGrid1 , dataGrid.Cell[6] 是超链接列在 dataGrid1的ItemDataBound事件里
private void dataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{if(e.Item.ItemIndex > -1)
{
System.Web.UI.WebControls.HyperLink hlc = (System.Web.UI.WebControls.HyperLink) e.Item.Cells[19].Controls[0];
hlc.NavigateUrl ="你要链接的页面";
}}
将超连接列换成模板列
<TemplateItem>
<Span style="cursor:hand" onclick='todo("<%#DataBinder.Eval(Container,"在此替换成你要判断的数据列")%>")'><%#DataBinder.Eval(Container,"在此替换成你要显示的数据列")%></Span>
</TemplateItem>
在aspx页面中加入
<Script language=javascript>
function todo(values)
{
if( values == '')
//在此处理你想要的情况
}
</Script>
<%@ Page %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form method=post runat="server">
<asp:DataGrid runat="server" ID="Datagrid1"
DataKeyField="EmployeeID"
OnEditCommand="Datagrid1_Edit"
OnUpdateCommand="Datagrid1_Update"
OnCancelCommand="Datagrid1_Cancel"
OnDeleteCommand="Datagrid1_Delete"
AutoGenerateColumns="False"
Width="100%"
HeaderStyle-Font-Size="10"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="Red"
HeaderStyle-BackColor="Yellow"
HeaderStyle-BorderColor="Red"
HeaderStyle-BorderWidth="5"
FooterStyle-BorderColor="Red"
FooterStyle-BorderWidth="5"
ItemStyle-BackColor="LightCyan"
ItemStyle-ForeColor="DarkBlue"
AlternatingItemStyle-BackColor="LightYellow"
AlternatingItemStyle-ForeColor="Maroon">
<Columns>
<asp:EditCommandColumn ItemStyle-Width="25px"
EditText="<img border=0 alt='Edit record' src=Edit.gif>"
CancelText="<img border=0 alt='Cancel changes' src=Cancel.gif>"
UpdateText="<img border=0 alt='Accept changes' src=OK.gif>"
/>
<asp:ButtonColumn ItemStyle-Width="25px" ButtonType="LinkButton"
Text="<img border=0 src=Delete.gif>" CommandName="delete"
/>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID" ReadOnly="true" />
<asp:TemplateColumn HeaderText="Title" ItemStyle-Width="50px">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "TitleOfCourtesy") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="EditTitle" DataSource='<%# TitlesOfCourtesy %>'
SelectedIndex='<%# GetSelectedTitle(DataBinder.Eval(Container.DataItem, "TitleOfCourtesy")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<b><%# DataBinder.Eval(Container.DataItem, "LastName") %></b>,
<%# DataBinder.Eval(Container.DataItem, "FirstName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditLastName" Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>' />
<asp:TextBox runat="server" ID="EditFirstName" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="City" ItemStyle-Width="150px" DataField="City" />
<asp:TemplateColumn HeaderText="USA?" ItemStyle-Width="35px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox runat="server" Enabled="false"
Checked='<%# DataBinder.Eval(Container.DataItem, "Country").ToString() == "USA" %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html><script runat="server" language="C#">
public string[] TitlesOfCourtesy
{
get {
return new string[4]{"Mr.", "Dr.", "Ms.", "Mrs."};
}
}
int GetSelectedTitle(object title)
{
return Array.IndexOf(TitlesOfCourtesy, title.ToString());
}
void Page_Load()
{
if (!Page.IsPostBack)
BindGrid();
}
void BindGrid()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = @"SELECT * FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
// bind the reader to the DataList
Datagrid1.DataSource = reader;
Datagrid1.DataBind();
// close the reader and the connection
reader.Close();
conn.Close();
}
void Datagrid1_Edit(Object sender, DataGridCommandEventArgs e)
{
Datagrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
void Datagrid1_Cancel(Object sender, DataGridCommandEventArgs e)
{
Datagrid1.EditItemIndex = -1;
BindGrid();
} void Datagrid1_Update(Object sender, DataGridCommandEventArgs e)
{
// get the ID of the record to update
int empID = (int)Datagrid1.DataKeys[e.Item.ItemIndex];
// get the references to the edit controls
DropDownList title = (DropDownList)e.Item.FindControl("EditTitle");
TextBox lastName = (TextBox)e.Item.FindControl("EditLastName");
TextBox firstName = (TextBox)e.Item.FindControl("EditFirstName");
TextBox city = (TextBox)e.Item.Cells[5].Controls[0];
// create the connection and the UPDATE command
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = @"UPDATE Employees SET TitleOfCourtesy = @TitleOfCourtesy,
LastName = @LastName, FirstName = @FirstName, City = @City WHERE EmployeeID = @EmployeeID";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
// create the required parameters
cmd.Parameters.Add(new SqlParameter("@TitleOfCourtesy", SqlDbType.NVarChar, 25));
cmd.Parameters["@TitleOfCourtesy"].Value = title.SelectedItem.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 20));
cmd.Parameters["@LastName"].Value = lastName.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 10));
cmd.Parameters["@FirstName"].Value = firstName.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 15));
cmd.Parameters["@City"].Value = city.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Value = empID;
// execute the command
cmd.Connection.Open();
try {
cmd.ExecuteNonQuery();
}
catch (SqlException) {
// handle exception...
}
finally {
cmd.Connection.Close();
}
// stop the editing and rebind the grid
Datagrid1.EditItemIndex = -1;
BindGrid();
}
void Datagrid1_Delete(Object sender, DataGridCommandEventArgs e)
{
// get the ID of the record to update
int empID = (int)Datagrid1.DataKeys[e.Item.ItemIndex];
// create the connection and the DELETE command
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = @"DELETE FROM Employees WHERE EmployeeID = " + empID.ToString();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
// execute the command
cmd.Connection.Open();
try {
cmd.ExecuteNonQuery();
}
catch (SqlException) {
// handle exception...
}
finally {
cmd.Connection.Close();
}
// rebind the grid
Datagrid1.EditItemIndex = -1;
BindGrid();
}
</script>
以上是类似的模板代码,你可以改一些,比如把里面的<asp:DropDownList>控件改成
<asp:LinkButton>把URL动态的设为'<% 服务器端的变量 %>'.
OK了.
2.在显示完毕做