|
在一张页面有个GridView于数据库绑定了,在GridView中添加一列.我们设置为回复。
功能即电击其中一行后的回复,弹出一个对话框,可以对以前这条数据的回复做修改。
开始:
第一步:添加回复按钮,代码为
<asp:LinkButton ID="lbtnCheck" runat="server" CausesValidation="false" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"OrderId") %>' OnClientClick='<%# GetCommentCommand((DataBinder.Eval(Container.DataItem,"FailComment")).ToString()) %>' CommandName="Check" Text='回复' Visible='<%# IsComment(long.Parse((Eval("OrderId")).ToString()))==0? false:true %>'> </asp:LinkButton>
其中OnClientClick事件调用JAVASCRIPT脚本,GetCommentCommand是一个函数,我们先把他们代码写出来
还得在页面加一个隐藏控件hfComment,他是为了接受等会窗体传会的值
第二步:单击按钮会先调用函数
protected string GetCommentCommand(string strComment) { return "commentView('" + strComment + "')"; }
这个函数是必须要写的,因为我们要向小窗体传递参数,而那个参数textComment在表里的类型不能确定,如果它是字符型那么传递过去后必须给字符加上引号。。否则会出错的,函数调用脚本
脚本会弹出一个对话框,并且给它传递一个值FailComment
function commentView(textComment) { var url='EditFailOrderComment.aspx?FailComment='+textComment; var comment=window.showModalDialog(encodeURI(url),window,'dialogHeight=250px;dialogWidth=330px;Status=no;'); if(comment != null) { document.getElementById("hfComment").value=comment; } }
第3步在小窗体中获取查询字符串传递来的值
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string strTxtComment = null; //获取现有的回复 strTxtComment = (Request.QueryString["FailComment"] == null) ? null : Request.QueryString["FailComment"]; txtComment.Text = strTxtComment; } }
第4步:给小窗体的确定和取消按钮添加单击事件
我们用的是脚本
function Ok() { var txt = document.getElementById("txtComment"); //获取传递过来显示在文本框上的值 var comment = null; comment = txt.value; if(comment == 0) { alert("未输入任何回复!"); return; } window.returnValue = comment; //将输入的回复返回给原来的页面,它存放在隐藏孔件hfComment window.close(); } function Cancel() { window.returnValue = null; window.close(); }
最后第5步:
在页面GridView中的RowCommand事件中添加获取传递回来的值,然后添加进数据库的代码
if(e.CommandName=="Check")
{
//............................省略
}
这样们的功能就能实现了,
其中最主需要注意的地方还是那个给传递的值加上引号。
其他没什么要讲的,希望我写下的问题大家如果有谁遇到可以注意吧。
|