[英]ASP TextBox extra values after postback
In summary: I'm saving grid id and row number to some hidden textboxes inside a GridView so I can update the session data via jQuery. 总结:我将网格ID和行号保存到GridView中一些隐藏的文本框中,以便可以通过jQuery更新会话数据。 After a drag and drop operation on the GridView, one of the textboxes holds different data than it should. 在GridView上进行拖放操作后,其中一个文本框保存的数据与实际值不同。 Does anyone know why this might be? 有谁知道为什么会这样吗?
Edit: solved by doing the databinding in Page_PreRender instead of Page_Load. 编辑:通过在Page_PreRender而不是Page_Load中进行数据绑定来解决。
I'm trying to build a page with 2 draggable and sortable GridViews. 我正在尝试用2个可拖动和可排序的GridViews构建页面。 This is a 'teach myself jQuery in preparation for using those methods on production pages' kind of project. 这是一种“自学jQuery,准备在生产页面上使用这些方法的项目”。 When a row is dragged and dropped, it calls a postback to update the underlying datasets and rebind the grids. 拖放行时,它将调用回发以更新基础数据集并重新绑定网格。 Users should be able to reorder the gridviews and also drag rows from one to the other. 用户应该能够重新排列网格视图,还可以将行从一个拖动到另一个。 I get the feeling that I'm making it way harder than it should be, but that's not my question. 我感觉到我正在使它变得比应有的难度更大,但这不是我的问题。
To make the postbacks work, I'm creating 2 hidden textboxes that store the grid id and row number on each row. 为了使回发有效,我正在创建2个隐藏的文本框,它们在每行上存储网格ID和行号。 jQuery uses those as parameters to pass to the code-behind via a PageMethods call. jQuery使用这些参数作为参数,通过PageMethods调用传递给背后的代码。 All of that works, the first time. 所有这些都是第一次。
If I try to do a drag-and-drop on a row I already dragged and dropped once, the row number textbox.text field becomes x,x instead of x like the other rows. 如果我尝试对已经拖放一次的行进行拖放,则行号textbox.text字段将变为x,x,而不是其他行。 For instance, dragging row 1 somewhere makes row 1's TextBox.Text become 1,1. 例如,将第1行拖到某处会使第1行的TextBox.Text变为1,1。 I've verified that in RowDataBound the number is 1 and at Page_Unload it's 1,1. 我已验证,在RowDataBound中,该数字为1,在Page_Unload中为1,1。 Why is this? 为什么是这样?
Javascript: Javascript:
<script type="text/javascript">
$(document).ready(function () {
$( ".draggable" ).draggable({
helper: "clone",
stack: ".draggable",
snapto: ".droppable",
create: function(event, ui){
var GridID = $(this).find(".gridid").attr("value");
$(this).data("source",GridID);
var RowID = $(this).find(".rowindex").attr("value");
$(this).data("rowid",RowID);
}
});
$( ".droppable" ).droppable({
tolerance: "intersect",
greedy: true,
create: function(event, ui){
var GridID = $(this).find(".gridid").attr("value");
$(this).data("source",GridID);
var RowID = $(this).find(".rowindex").attr("value");
$(this).data("rowid",RowID);
},
drop: function(event, ui){
var SourceGrid = ui.draggable.data("source");
var SourceRow = ui.draggable.data("rowid");
var DestGrid = $(this).data("source");
var DestRow = $(this).data("rowid");
PageMethods.MoveRow(SourceGrid, DestGrid, SourceRow, DestRow);
__doPostBack('','');
}
});
});
</script>
ASP: ASP:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" runat="Server">
<asp:GridView ID="gvSource" runat="server" ShowFooter="true"
OnRowDataBound="gvSource_RowDataBound">
</asp:GridView>
<asp:GridView ID="gvDest" runat="server" ShowFooter="true"
OnRowDataBound="gvSource_RowDataBound">
</asp:GridView>
Code-Behind (minus the DataBinding and fetching parts): 后台代码(减去DataBinding和访存部分):
protected void gvSource_RowDataBound(object sender, GridViewRowEventArgs e) {
TextBox gridid = new TextBox();
gridid.Text = ((GridView)sender).ClientID;
gridid.CssClass = "gridid hidden";
TextBox rowindex = new TextBox();
switch (e.Row.RowType) {
case DataControlRowType.DataRow:
rowindex.Text = e.Row.RowIndex.ToString();
break;
case DataControlRowType.Header:
rowindex.Text = "0";
break;
case DataControlRowType.Footer:
rowindex.Text = ((DataTable)((GridView)sender).DataSource).Rows.Count.ToString();
break;
default:
rowindex.Text = "null";
break;
}
rowindex.CssClass = "rowindex hidden";
e.Row.Cells[0].Controls.Add(gridid);
e.Row.Cells[0].Controls.Add(rowindex);
}
[WebMethod]
public static string MoveRow(string _sourcegrid, string _destgrid, string _sourcerow, string _destrow) {
HttpContext ctx = HttpContext.Current;
DataTable dtsrc = _sourcegrid == ((DataTable)ctx.Session["dtFrom"]).TableName ? (DataTable)ctx.Session["dtFrom"] : (DataTable)ctx.Session["dtTo"];
DataTable dtdest = _destgrid == ((DataTable)ctx.Session["dtFrom"]).TableName ? (DataTable)ctx.Session["dtFrom"] : (DataTable)ctx.Session["dtTo"];
DataRow row = dtsrc.Rows[Convert.ToInt32(_sourcerow)];
DataRow newrow = dtdest.NewRow();
int newrowpos = Convert.ToInt32(_destrow);
newrow.ItemArray = row.ItemArray;
dtsrc.Rows.Remove(row);
dtdest.Rows.InsertAt(newrow, newrowpos);
return "1";
}
CSS-wise, all rows have CssClass="droppable draggable". 在CSS方面,所有行均具有CssClass =“ droppable draggable”。 Headers and footers are just droppable. 页眉和页脚只是可放置的。 I left off some error checking code for brevity. 为了简洁起见,我省略了一些错误检查代码。
edit: added a summary, and I wanted to add that I've looked through SO and found only topics about a TextBox losing its data, not changing it. 编辑:添加了一个摘要,我想补充一点,就是我仔细观察了一下,发现仅有关TextBox丢失其数据而不更改它的主题。
The problem had something to do with binding the data in Page_Load. 问题与绑定Page_Load中的数据有关。 It looked like 看起来像
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
GetData(); //Database fetch saves 2 DataTables to session variables
}
gvSource.DataSource = (DataTable)Session["dtFrom"];
gvSource.DataBind();
gvDest.DataSource = (DataTable)Session["dtTo"];
gvDest.DataBind();
}
and it failed. 失败了 Made it 做好了
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
GetData(); //Database fetch
}
}
protected void Page_PreRender(object sender, EventArgs e) {
gvSource.DataSource = (DataTable)Session["dtFrom"];
gvSource.DataBind();
gvDest.DataSource = (DataTable)Session["dtTo"];
gvDest.DataBind();
}
and it worked. 而且有效。 Why it worked is still open for debate. 为何有效,仍有待辩论。 Thanks guys for helping this long-time lurker with basic SO usage and etiquette. 谢谢大家通过基本的SO使用和礼节帮助这个长期潜伏者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.