我在ConsumerList页一个GridView。当点击编辑按钮(每一行中),它被导航到一个EditConsumer页。在EditConsumer页面,我需要使用网页。previousPage ,以获得所选行的值。如何获得这些价值?
公共无效Consumer_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
如果(e.CommandName ==CustomEdit)
{
INT rowIndex位置= Convert.ToInt32(e.CommandArgument);
字符串consumerID = grdConsumers.Rows [rowIndex位置] .Cells [1]。文;
字符串consumerName = grdConsumers.Rows [rowIndex位置] .Cells [2]。文;
字符串consumerUrl = grdConsumers.Rows [rowIndex位置] .Cells [3]。文;
字符串consumerStatus = grdConsumers.Rows [rowIndex位置] .Cells [4]。文;
的Response.Redirect(EditConsumer.aspx rowIndex位置=?+ rowIndex位置);
}
}
MARKUP
< ASP:GridView控件ID =grdConsumers=服务器的AutoGenerateColumns =假的CssClass =resultGridTable
网格线=无的EnableViewState =真AllowSorting =真OnSorting =Consumers_SortingOnRowCommand =Consumer_RowCommand>
< AlternatingRowStyle背景色=#E5E5E5/>
<列>
< ASP:模板列HEADERTEXT =行动>
<的ItemTemplate>
< ASP:按钮的ID =btnView=服务器的CssClass =actionButtonView
文本=查看风格=宽度:40PX背景色=橙色
字体粗体=真前景色=白/>
< ASP:按钮的ID =btnEdit=服务器的CommandName =CustomEditCommandArgument =<%#((GridViewRow)集装箱).RowIndex%>中
的CssClass =actionButtonEdit
文本=编辑风格=宽度:35px背景色=橙色
字体粗体=真前景色=白/>
< ASP:按钮的ID =btnDelete=服务器的CssClass =actionButtonDelete
文本=删除风格=宽度:55像素背景色=橙色
字体粗体=真前景色=白/>
< ASP:按钮的ID =btnPing=服务器的CssClass =actionButtonPing
文本=PING风格=宽度:35px背景色=橙色
字体粗体=真前景色=白/>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:绑定列HEADERTEXT =消号数据字段=ConsumerIDSORTEX pression =ConsumerID>< / ASP:绑定列>
< ASP:绑定列HEADERTEXT =消费者权益名称数据字段=ConsumerNameSORTEX pression =ConsumerName>< / ASP:绑定列>
< ASP:绑定列HEADERTEXT =消费者URL数据字段=ConsumerURLSORTEX pression =ConsumerURL>< / ASP:绑定列>
< ASP:绑定列HEADERTEXT =状态数据字段=状态SORTEX pression =状态>< / ASP:绑定列>
< /列>
< / ASP:GridView控件>
解决方案
注意: previousPage
仅!= NULL
与跨页投递 ,的Response.Redirect 会不行。因此,例如,你可以使用 的Server.Transfer
。
您可以提供一个公共财产 SelectedConsumer
,返回curerently选定行中的的GridView
自定义对象。
大众消费者SelectedConsumer
{
得到 {
如果(grdConsumers.SelectedRow == NULL)
返回null;
变种C =新客户();
c.consumerID = grdConsumers.SelectedRow.Cells [1]。文;
c.consumerName = grdConsumers.SelectedRow.Cells [2]。文;
c.consumerUrl = grdConsumers.SelectedRow.Cells [3]。文;
c.consumerStatus = grdConsumers.SelectedRow.Cells [4]。文;
返回℃;
}
}
现在,你只需要在 previousPage
属性转换为正确的页面类型:
VAR consumerList =(ConsumerList)页previousPage。
VAR selectedConsumer = consumerList.SelectedConsumer;
I have a gridView in ConsumerList page. When Edit button is clicked (in each row), it is navigated to a EditConsumer page. In the EditConsumer page, I need to get the values of the selected row using Page.PreviousPage. How to get these values?
public void Consumer_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "CustomEdit")
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
string consumerID = grdConsumers.Rows[rowIndex].Cells[1].Text;
string consumerName = grdConsumers.Rows[rowIndex].Cells[2].Text;
string consumerUrl = grdConsumers.Rows[rowIndex].Cells[3].Text;
string consumerStatus = grdConsumers.Rows[rowIndex].Cells[4].Text;
Response.Redirect("EditConsumer.aspx?RowIndex=" + rowIndex);
}
}
MARKUP
<asp:GridView ID="grdConsumers" runat="server" AutoGenerateColumns="False" CssClass="resultGridTable"
GridLines="None" EnableViewState="True" AllowSorting="True" OnSorting="Consumers_Sorting" OnRowCommand="Consumer_RowCommand">
<AlternatingRowStyle BackColor="#E5E5E5" />
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnView" runat="server" CssClass="actionButtonView"
Text="VIEW" Style="width: 40px" BackColor="Orange"
Font-Bold="True" ForeColor="White" />
<asp:Button ID="btnEdit" runat="server" CommandName="CustomEdit" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
CssClass="actionButtonEdit"
Text="EDIT" Style="width: 35px" BackColor="Orange"
Font-Bold="True" ForeColor="White" />
<asp:Button ID="btnDelete" runat="server" CssClass="actionButtonDelete"
Text="DELETE" Style="width: 55px" BackColor="Orange"
Font-Bold="True" ForeColor="White" />
<asp:Button ID="btnPing" runat="server" CssClass="actionButtonPing"
Text="PING" Style="width: 35px" BackColor="Orange"
Font-Bold="True" ForeColor="White" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Consumer ID" DataField="ConsumerID" SortExpression="ConsumerID"></asp:BoundField>
<asp:BoundField HeaderText="Consumer Name" DataField="ConsumerName" SortExpression="ConsumerName"></asp:BoundField>
<asp:BoundField HeaderText="Consumer URL" DataField="ConsumerURL" SortExpression="ConsumerURL"></asp:BoundField>
<asp:BoundField HeaderText="Status" DataField="Status" SortExpression="Status"></asp:BoundField>
</Columns>
</asp:GridView>
解决方案
Note: PreviousPage
is only != null
with cross page posting, Response.Redirect will not work. So you could for example use Server.Transfer
.
You could provide a public property SelectedConsumer
that returns a custom object of the curerently selected row in the GridView
.
public Consumer SelectedConsumer
{
get {
if (grdConsumers.SelectedRow == null)
return null;
var c = new Consumer();
c.consumerID = grdConsumers.SelectedRow.Cells[1].Text;
c.consumerName = grdConsumers.SelectedRow.Cells[2].Text;
c.consumerUrl = grdConsumers.SelectedRow.Cells[3].Text;
c.consumerStatus = grdConsumers.SelectedRow.Cells[4].Text;
return c;
}
}
Now you only have to cast the PreviousPage
property to the correct page type:
var consumerList = (ConsumerList)Page.PreviousPage;
var selectedConsumer = consumerList.SelectedConsumer;