目前我有一个GridView,其中包含一个带日期的列。此列在每个单元格中都有一个链接,用于打开一个窗口并显示一个aspx页面。 我想要它做的是让链接打开一个窗口,并在新的GridView中显示该特定行和其他信息的日期。
这是我的代码:
ASCX
<asp:GridView ID="GridView1" runat="server" CssClass="summary1" DataSourceID="SqlDataSource2"
AllowPaging="True" AutoGenerateColumns="False" AllowSorting="True" EmptyDataText="No Records Found..." >
<AlternatingRowStyle CssClass="altrow"/>
<HeaderStyle CssClass="ui-state-default" />
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate> <%--edited the javascript--%>
<a href="javascript:showDateProfile('%# Eval('DateFormat') %')" id="link1" runat="server" class="underline"><%# Eval("DateFormat") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Orders" HeaderText="Orders" SortExpression="Orders" />
<asp:BoundField DataField="Cancels" HeaderText="Cancels" SortExpression="Cancels" />
<asp:BoundField DataField="Net" HeaderText="Net" SortExpression="Net" />
<asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" />
</Columns>
</asp:GridView>
javascript(在aspx文件中) - 已编辑
<script type="text/javascript">
function showDateProfile(x) {
var w1 = window.open("DateProfile.aspx?id=x", "Profile", "width=800,height=600");
}
// edit
var qs = this.Request.QueryString["x"];
</script>
我非常感谢如何让它按照我想要的方式工作。谢谢你提前。
编辑
这是在新窗口中为gridview执行数据源的好方法吗?
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
SelectCommand="SELECT uid, name, company, startdate, enddate FROM Customer WHERE (startdate = @Date) or (enddate = @Date)"
ConnectionString="<%$ ConnectionStrings:New_QuickBooksConnectionString %>">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="Date" QueryStringField="DateFormat" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>
答案 0 :(得分:1)
我为您编写了一个示例,其中显示了所有persons
,并通过点击BirthDate
列,用户被重定向到另一个页面,
这是主页面代码前端(WebForm1.aspx):
<script>
function showDateProfile(x) {
window.open("DateProfile_1.aspx?id=" + x, "Profile", "width=800,height=600");
};
</script>
<asp:GridView ID="PersonGridView_Master"
runat="server"
AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="BirthDate">
<ItemTemplate>
<a href="javascript:showDateProfile('<%#Eval("id")%>')"><%# Eval("date")%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="Name"/>
</Columns>
</asp:GridView>
我没有DataSource控件,因为我绑定了代码后面的数据(WebForm1.aspx.cs),细节无关紧要
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{
// you can bind data to your grid in different ways
// SqlDataSource, ObjectDataSource, ... , it doesn`t matter
PersonGridView_Master.DataSource = GetPersons();
PersonGridView_Master.DataBind();
}
public object GetPersons()
{
// you can fetch your data in different ways, it doesn`t matter
TestEntities context = new TestEntities();
return context.tbl_Persons
.Select(u => new
{
id = u.fldID,
name = u.fldFirstName,
date = u.fldBirthDate
})
.ToList();
}
现在,在第二页...... 这是我的第二页代码前端(DateProfile_1.aspx),
<asp:GridView ID="PersonGridView_Detail"
runat="server"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="date" HeaderText="BirthDate"/>
<asp:BoundField DataField="name" HeaderText="Name"/>
</Columns>
</asp:GridView>
而且在这里,我没有任何数据源控件,因为我绑定了代码中的数据... 这是(DateProfile_1.aspx.cs)
背后的代码public partial class DateProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// get from query string and casting to its type
Guid id = Guid.Parse(Request.QueryString["id"]);
// populate grid
BindGridToPerson(id);
}
public void BindGridToPerson(Guid ID)
{
// you can bind data to your grid in different ways
// if you can use SqlDataSource, ObjectDataSource, you need to set property on those
// to say that they need to get id from query string, in this way, we bind data
// to grid manually
PersonGridView_Detail.DataSource = GetPersonByID(ID);
PersonGridView_Detail.DataBind();
}
public object GetPersonByID(Guid ID)
{
// you can fetch your data in different ways, it doesn`t matter
TestEntities context = new TestEntities();
return context.tbl_Persons
.Select(u => new
{
id = u.fldID,
name = u.fldFirstName,
date = u.fldBirthDate
})
.Where(u => u.id == ID)
.ToList();
}
}
并且我有另一个页面使用SqlDataSource
绑定数据,在这种情况下,你不需要任何服务器端代码,因为这个共振,我只发布代码前面(DateProfile_2.aspx):
<asp:GridView ID="PersonGridView_Detail"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="date" HeaderText="BirthDate"/>
<asp:BoundField DataField="name" HeaderText="Name"/>
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Web_TestConnectionString %>"
SelectCommand="SELECT [fldID], [fldBirthDate] as date, [fldFirstName] as name
FROM [Common].[tbl_Persons]
where [fldID] = @id">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
</SelectParameters>
</asp:SqlDataSource>
结束,让我知道,如果这篇文章对你有用
答案 1 :(得分:0)
首先你需要将你的唯一标识符传递给showDateProfile()
以传递到第二页,为此,你可以在这个标记中定义<a>
元素
<a href="javascript:showDateProfile('<%# Eval("YOUR_UNIQUE_INDETIFIER_FIELD_NAME") %>')" id="link1" runat="server" class="underline"><%# Eval("DateFormat") %></a>
然后您需要将showDateProfile()
中的ID作为Query String
传递到第二页,如下所示:
function showDateProfile(id) {
var w1 = window.open("DateProfile.aspx?x="id, "Profile", "width=800,height=600");
}
然后在第二个page_load事件中,您可以像这样获得唯一标识符:
this.Request.QueryString["x"]
并转换为其类型,例如,如果您的ID类型为Guid
,则需要执行此操作:Guid.Parse(this.Request.QueryString["x"])
然后,你有了id,唯一的办法是从你的数据库中获取信息并用任何东西显示它们,比如GridView