我只想创建一个按钮" view"单击gridview特定行中的数据并将其传递给Silverlight查看器。 这是" viewall"按钮我试图弄清楚。
protected void btnViewAll_Click(object sender, EventArgs e)
{
string id = HttpContext.Current.Request.Headers["id"];
#if DEBUG
if (id == null)
id = "111"; // Use my own id for testing locally
#endif
using (aDataContext dc = new aDataContext())
{
var query = (from a in dc.aLists
where a.ID == id
select new
{
a.aNumber,
a.bNumber,
a.cNumber,
a.dNumber,
}
);
List<aListItem> List = new List<aListItem>();
foreach (var queryItem in query)
{
aList.Add(new aListItem()
{
aNumber = queryItem.aNumber,
bNumber = queryItem.bNumber,
cNumber = queryItem.cNumber,
dNumber = queryItem.dNumber
});
}
Session["aList"] = aList;
}
Session["Database"] = null;
Response.Redirect("Viewer.aspx" + "?UseList=true");
}
答案 0 :(得分:0)
解决方案1:
可以使用GridView中的ButtonField
来完成:
将行特定信息添加到GridView的DataKeyNames
(例如,如果需要使用Silverlight查看器查看数据源的字段“name1”和“age2”):
<asp:GridView ID="gvClients" DataKeyNames="name1,age2" ... >
将ButtonField添加到GridView:
<asp:ButtonField ButtonType="Button" CommandName="View" Text="View" />
设置RowCommand事件处理程序(可以在标记或代码中完成):
gvClients.RowCommand += new GridViewCommandEventHandler(gvClients_RowCommand);
处理命令:
void gvClients_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
string name1 = (string)gvClients.DataKeys[rowIndex].Values["name1"];
int age2 = (int)gvClients.DataKeys[rowIndex].Values["age2"];
// Open docName with the viewer
...
}
解决方案2:另一种避免回发的方法是使用HyperLinkField:
<asp:HyperLinkField Text="View" DataNavigateUrlFormatString="Viewer.aspx?Name={0}&Age={1}" DataNavigateUrlFields="name1,age2" />