使用 ajax 在 asp.net 中的 datagridview 中可单击的行
我使用 ajax 在数据网格中实现了可点击的行。问题是,当我单击“结果”页面中的行时,我会重定向到另一个页面以查看数据。在该页面中,我有一个“更新”按钮,我可以更新数据库中表中的数据,当我单击它时,我重定向回结果页面,然后再次单击同一行以查看其详细信息,数据不是已更新且仍然是旧数据。 问题是,数据正在数据库的表中更新,就像页面正在保存他带来的旧数据并使用它,我不知道如何告诉他带来新数据。
这是创建可点击行的代码:
protected void pendingGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "return GetDataUsingAJAX(" + e.Row.RowIndex + ",'pending');");
e.Row.Attributes.Add("style", "cursor:hand");
}
}
这是结果页面的 javascript:
function GetDataUsingAJAX(row, table) {
obj = new XMLHttpRequest();
if (obj != null) {
obj.onreadystatechange = RedirectToViewDetails;
obj.open("GET", "Results2.aspx?row=" + row + "&table=" + table, true);
obj.send(null);
}
return false;
}
function RedirectToViewDetails() {
if (obj.readyState == 4) {
if (obj.status == 200) {
var retval = obj.responseText.split("&");
window.location = "YellowCardStart.aspx?mode=" + retval[0] + "&cntct=" + retval[1] + "&strtDate=" + retval[2] + "&endDate=" + retval[3] + "&strtTime=" + retval[4] + "&endTime=" + retval[5] + "&tools=" + retval[6] + "&id=" + retval[7] + "&table=" + retval[8] + "&bldng=" + retval[9] + "&loc=" + retval[10] + "&devTool=" + retval[11] + "&cmpny=" + retval[12] + "&phn=" + retval[13] + "&lssApp=" + retval[14] + "&ehsApp=" + retval[15] + "&cmnts=" + retval[16] + "&created=" + retval[17]; }
else {
alert("Error retrieving data!");
}
}
}
这是结果页面加载中的代码:
if (Request.QueryString["row"] != null)
{
Response.Clear();
if (Request.QueryString["table"] != null)
{
DataTable dt = new DataTable();
if (Request.QueryString["table"] == "fa")
{
dt = DataAccessLayer.selectFromTable(Int32.Parse(activeFAGrid.Rows[Int16.Parse(Request.QueryString["row"])].Cells[0].Text), "active");
Response.Write(dt.Rows[0][1].ToString() + "&" + dt.Rows[0][2].ToString() + "&" + dt.Rows[0][3].ToString() + "&" + dt.Rows[0][4].ToString() + "&" + dt.Rows[0][5].ToString() + "&" + dt.Rows[0][6].ToString() + "&" + dt.Rows[0][7].ToString() + "&" + dt.Rows[0][0].ToString() + "&" + "active" + "&" + dt.Rows[0][8].ToString() + "&" + dt.Rows[0][9].ToString() + "&" + dt.Rows[0][10].ToString() + "&" + dt.Rows[0][11].ToString() + "&" + dt.Rows[0][12].ToString() + "&" + dt.Rows[0][13].ToString() + "&" + dt.Rows[0][14].ToString() + "&" + dt.Rows[0][15].ToString() + "&" + dt.Rows[0][16].ToString());
}
Response.End();
}
}
SelectFromTable 函数从数据库检索数据。
帮助将非常感激,我真的迷失了。
提前致谢,
格雷格
I implemented clickable rows in datagrid using ajax. The problem is, when I click the row in the Results page I redirect to another page to view the data. In that page I have an "update" button that I can update the data in the table in the DB and when I click it, I redirect back to the Results page and then click the same row again to view its details, the data isnt updated and its still old data.
The thing is, the data is being updated in the table in the DB, its like the page is saving the old data he brought and using it and I dont know how to tell him to bring new data.
Here is the code to make clickable rows:
protected void pendingGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "return GetDataUsingAJAX(" + e.Row.RowIndex + ",'pending');");
e.Row.Attributes.Add("style", "cursor:hand");
}
}
here is the javascript of Results page:
function GetDataUsingAJAX(row, table) {
obj = new XMLHttpRequest();
if (obj != null) {
obj.onreadystatechange = RedirectToViewDetails;
obj.open("GET", "Results2.aspx?row=" + row + "&table=" + table, true);
obj.send(null);
}
return false;
}
function RedirectToViewDetails() {
if (obj.readyState == 4) {
if (obj.status == 200) {
var retval = obj.responseText.split("&");
window.location = "YellowCardStart.aspx?mode=" + retval[0] + "&cntct=" + retval[1] + "&strtDate=" + retval[2] + "&endDate=" + retval[3] + "&strtTime=" + retval[4] + "&endTime=" + retval[5] + "&tools=" + retval[6] + "&id=" + retval[7] + "&table=" + retval[8] + "&bldng=" + retval[9] + "&loc=" + retval[10] + "&devTool=" + retval[11] + "&cmpny=" + retval[12] + "&phn=" + retval[13] + "&lssApp=" + retval[14] + "&ehsApp=" + retval[15] + "&cmnts=" + retval[16] + "&created=" + retval[17]; }
else {
alert("Error retrieving data!");
}
}
}
And here is the code in page_load of Results:
if (Request.QueryString["row"] != null)
{
Response.Clear();
if (Request.QueryString["table"] != null)
{
DataTable dt = new DataTable();
if (Request.QueryString["table"] == "fa")
{
dt = DataAccessLayer.selectFromTable(Int32.Parse(activeFAGrid.Rows[Int16.Parse(Request.QueryString["row"])].Cells[0].Text), "active");
Response.Write(dt.Rows[0][1].ToString() + "&" + dt.Rows[0][2].ToString() + "&" + dt.Rows[0][3].ToString() + "&" + dt.Rows[0][4].ToString() + "&" + dt.Rows[0][5].ToString() + "&" + dt.Rows[0][6].ToString() + "&" + dt.Rows[0][7].ToString() + "&" + dt.Rows[0][0].ToString() + "&" + "active" + "&" + dt.Rows[0][8].ToString() + "&" + dt.Rows[0][9].ToString() + "&" + dt.Rows[0][10].ToString() + "&" + dt.Rows[0][11].ToString() + "&" + dt.Rows[0][12].ToString() + "&" + dt.Rows[0][13].ToString() + "&" + dt.Rows[0][14].ToString() + "&" + dt.Rows[0][15].ToString() + "&" + dt.Rows[0][16].ToString());
}
Response.End();
}
}
the SelectFromTable function retrieves the data from the DB.
Help will be really appriciated, I am really lost.
Thanks in advance,
Greg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您会发现您的浏览器正在缓存 AJAX 调用的响应,请尝试对 AJAX 调用使用 POST 而不是 GET。
I think you will find that your browser is caching the response from your AJAX call, try using POST instead of GET for your AJAX call.
只是为了让您知道,它使用“POST”工作,但我必须删除这两行才能使其工作:(
也许只删除第二行就足够了)
Just to let you know, it worked using "POST" but I had to remove these 2 lines in order for it to work:
(perhaps only removing the 2nd line could be enough)