问题描述,在一个UpdatePanel里使用GridView控件,其中有一列的ItemTemplate中放入一个TextBox控件和Button控件,我的需求是点击Button按钮,将TextBox中的内容存入数据库,并弹出保存成功对话框。简要代码<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" BorderColor="Black"
OnRowDataBound="GridView1_RowDataBound" runat="server"
AutoGenerateColumns="False" Font-Size="12px" Width="1200px" AllowSorting="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing" style="margin-bottom: 0px"
onrowcommand="GridView1_RowCommand" >
<Columns>
//省略其他列
<asp:TemplateField HeaderText="澄清回复">
<ItemTemplate>
<asp:TextBox ID="txtNBAnswer" runat="server" Width="200px" style='overflow:hidden;height:20px'
onpropertychange="this.style.height = this.scrollHeight + 'px';"
oninput="this.style.height = this.scrollHeight + 'px';"
TextMode="MultiLine" ></asp:TextBox>
<asp:Button ID="BttNBAnswer" runat="server"
CausesValidation="false" CommandArgument ="<%# GridView1.Rows.Count %>" CommandName="SaveNBAnswer" Text="Save" />
</ItemTemplate>
<HeaderStyle Width="220px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#E6F2FF" Font-Size="12px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
.cs代码 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SaveNBAnswer")
{
if (txtNBAnswer.Text == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "s2", "window.open('澄清回复不能为空,保存失败!');", true);
GridAllBind();
return;
}
int index = Convert.ToInt32(e.CommandArgument);//get Row index
DataKey datakey = GridView1.DataKeys[index];
string ClarityID = datakey["ID"].ToString();
string NBAnswer = ((TextBox)GridView1.Rows[index].FindControl("txtNBAnswer")).Text;
string NBAnswerDate = DateTime.Now.ToString();
string sqlStr = "insert into ClarityAnswer(ClarityID,Answer,AnswerPersonalID,AnswerDate) values('" + ClarityID + "','" + NBAnswer + "','" + PersonalID + "','" + NBAnswerDate + "')";
if (OperateDB.ExecCmd(sqlStr))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "s1", "window.open('保存成功!');", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "s1", "window.open('保存失败!');", true);
}
GridAllBind();
return;
}正常情况应该是弹出对话框,告知保存失败、保存成功或不能为空,可现在确是这样,如下图,请问如何解决?
<ContentTemplate>
<asp:GridView ID="GridView1" BorderColor="Black"
OnRowDataBound="GridView1_RowDataBound" runat="server"
AutoGenerateColumns="False" Font-Size="12px" Width="1200px" AllowSorting="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing" style="margin-bottom: 0px"
onrowcommand="GridView1_RowCommand" >
<Columns>
//省略其他列
<asp:TemplateField HeaderText="澄清回复">
<ItemTemplate>
<asp:TextBox ID="txtNBAnswer" runat="server" Width="200px" style='overflow:hidden;height:20px'
onpropertychange="this.style.height = this.scrollHeight + 'px';"
oninput="this.style.height = this.scrollHeight + 'px';"
TextMode="MultiLine" ></asp:TextBox>
<asp:Button ID="BttNBAnswer" runat="server"
CausesValidation="false" CommandArgument ="<%# GridView1.Rows.Count %>" CommandName="SaveNBAnswer" Text="Save" />
</ItemTemplate>
<HeaderStyle Width="220px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#E6F2FF" Font-Size="12px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
.cs代码 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SaveNBAnswer")
{
if (txtNBAnswer.Text == "")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "s2", "window.open('澄清回复不能为空,保存失败!');", true);
GridAllBind();
return;
}
int index = Convert.ToInt32(e.CommandArgument);//get Row index
DataKey datakey = GridView1.DataKeys[index];
string ClarityID = datakey["ID"].ToString();
string NBAnswer = ((TextBox)GridView1.Rows[index].FindControl("txtNBAnswer")).Text;
string NBAnswerDate = DateTime.Now.ToString();
string sqlStr = "insert into ClarityAnswer(ClarityID,Answer,AnswerPersonalID,AnswerDate) values('" + ClarityID + "','" + NBAnswer + "','" + PersonalID + "','" + NBAnswerDate + "')";
if (OperateDB.ExecCmd(sqlStr))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "s1", "window.open('保存成功!');", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "s1", "window.open('保存失败!');", true);
}
GridAllBind();
return;
}正常情况应该是弹出对话框,告知保存失败、保存成功或不能为空,可现在确是这样,如下图,请问如何解决?
解决方案 »
- 求:一个用CSS+DIV布局的asp.net网站的实例
- 如何把后台的数组赋值给前台JS的数组?
- 大家讨论一下,中国的软件中心在什么地方?什么地方对我们这行发展最有利?
- 在vs2005中,web窗体应用程序中.aspx设计中控件能不能随意拖放!
- .net2.0问题:如何得到(SELECT count(*) FROM 表)的返回值~
- Com组件不能注册,好急,在线等
- 当下载速度达到2.488Gbps的时候,我们还需要Ajax吗?
- 安装vs2003的问题?
- 请问后缀名为.resx的文件是什么类型的文件,在asp.net中有什么用呀?
- XmlDocument?????
- ASP.NET访问AD服务,急。。。。。。
- asp.net里获得存储过程输出的参数
alert('保存成功!');
alert就行了