none
【ASP.NET】框架iframe影响了gridview更新事件 RRS feed

  • 问题

  • 不知道为什么点击gridview按钮的编辑操作时  页面没有任何变化  唯独与以往不同的是该页面中有一个iframe框架  拿掉框架以后  点击编辑按钮却发现编辑可以实现 只不过是新谈出了一个页面来执行编辑状态   不知道什么什么原因  百思不得其解 请高手指教~谢谢


    刚才又检查了一下 原来是form表单的目标转向造成。由于页面要判断图片大小 所以我使用隐藏框架解决此类问题。最后查明是由于表单的指向造成的target="ajaxifr"   ajaxifr是框架iframe的id  如果却掉就可以了  不过去掉就影响了图片的大小判断。而后我又想到利用两个form来解决。第一个form中没有traget属性 代码为:

     

    <form runat="server" id="form1">
            <div style="margin-top: 30px;">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="785px"
                    OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                    OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:TemplateField HeaderText="编号">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server"><%#   Container.DataItemIndex   +   1%></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="30px" HorizontalAlign="Center" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="name" HeaderText="名称 ">
                            <ItemStyle Width="160px" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="图片地址">
                            <EditItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"path")%>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"path")%>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <a href='<%# ResolveUrl("~/"+Eval("path").ToString())%>' target="_blank">
                                    <img src='<%# ResolveUrl("~/"+Eval("path").ToString()) %>' alt='<%# Eval("name") %>'
                                        style="width: 30px; height: 40px; border: 0px;" /></a>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <div id="preview_fake" style="width: 80px; height: 85px; border: solid 1px #99BBE8;"
                                    runat="server">
                                    <img id="preview" style="width: 99px; height: 106px; border: 0px;" />
                                </div>
                                <input type="file" id="file" name="file" runat="server" onchange="perImg(this)" />&nbsp;
                                <span id="ajaxMsg" class="alert">大小不能超过1M</span>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="lbdelete" runat="server" CausesValidation="False" CommandName="Delete"
                                    Text="删除"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:LinkButton ID="lbupdate" runat="server" CausesValidation="True" CommandName="Update"
                                    Text="更新"></asp:LinkButton>
                                <asp:LinkButton ID="lbcancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                    Text="取消"></asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="lbedit" runat="server" CausesValidation="False" CommandName="Edit"
                                    Text="编辑"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        <span style="color: #ff0000">还没有数据!</span>
                    </EmptyDataTemplate>
                </asp:GridView>
                <br />
            </div>
        </form>

     

    第二个form 代码为:

     

     <form id="form2" enctype="multipart/form-data" method="post" target="ajaxifr">
            <table border="0" cellspacing="2" style="text-align: left; width: 785px;" cellpadding="2"
                bgcolor="#F3F2F3">
                <tr>
                    <td style="text-align: left; width: 6%;">
                        名称</td>
                    <td style="width: 563px">
                        <input type="text" name="name" id="name" style="width: 132px;" runat="server" onblur="nameResult();" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center; width: 6%;">
                    </td>
                    <td style="width: 563px" id="add">
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left; width: 6%;">
                        图片</td>
                    <td style="width: 563px">
                        <div id="preview_fake" style="width: 80px; height: 85px; border: solid 1px #99BBE8;"
                            runat="server">
                            <img id="preview" style="width: 99px; height: 106px; border: 0px;" />
                        </div>
                        <input type="file" id="file" name="file" runat="server" onchange="perImg(this)" />&nbsp;
                        <span id="ajaxMsg" class="alert">大小不能超过1M</span>
                    </td>
                </tr>
                <tr>
                    <td style="width: 6%">
                    </td>
                    <td style="width: 563px">
                    </td>
                </tr>
                <tr>
                    <td style="width: 6%">
                        &nbsp;</td>
                    <td style="width: 563px">
                        <asp:Button ID="btn1" runat="server" Text="增 加" Width="70px" OnClick="btn1_Click"
                            OnClientClick="return Check(this);"></asp:Button>
                    </td>
                </tr>
            </table>
        </form>

     

    这样的话又遇到问题  由于下面的button按钮是服务器控件,这样的话此form必须的加上runat属性。但一个页面只能有一个runat属性的表单会和以前的表单发生冲突。事情到此不知如何解决,忘请高手给予指点。小弟再次表示万分感谢!

    • 已移动 eryang 2011年3月7日 8:59 (发件人:.NET Framework 一般性问题讨论区)
    2011年3月6日 15:41

答案

  • 问题到此解决!现把处理方案加以总结归拢;

    由于一个页面不能使用两个带runat属性的表单。而gridview必须得在有runat的表单中,所以第一个form的代码为:<form id="form1" runat="server">
            <div style="margin-top: 30px;">
                <asp:GridView ID="GridView1" AutoGenerateColumns="False" Width="785px" runat="server"
                    OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                    OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:TemplateField HeaderText="编号">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server"><%#   Container.DataItemIndex   +   1%></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="30px" HorizontalAlign="Center" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="name" HeaderText="名称 ">
                            <ItemStyle Width="160px" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="图片地址">
                            <EditItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"path")%>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"path")%>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <a href='<%# ResolveUrl("~/"+Eval("path").ToString())%>' target="_blank">
                                    <img src='<%# ResolveUrl("~/"+Eval("path").ToString()) %>' alt='<%# Eval("name") %>'
                                        style="width: 30px; height: 40px; border: 0px;" /></a>
                            </ItemTemplate>
                            <%--<EditItemTemplate>
                                <div id="preview_fake1" style="width: 80px; height: 85px; border: solid 1px #99BBE8;"
                                    runat="server">
                                    <img id="preview1" style="width: 99px; height: 106px; border: 0px;" />
                                </div>
                                <input type="file" id="file1" name="file1" runat="server" onchange="perImg(this)" />&nbsp;
                                <span id="ajaxMsg" class="alert">大小不能超过1M</span>
                            </EditItemTemplate>--%>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="lbdelete" runat="server" CausesValidation="False" CommandName="Delete"
                                    Text="删除"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:LinkButton ID="lbupdate" runat="server" CausesValidation="True" CommandName="Update"
                                    Text="更新"></asp:LinkButton>
                                <asp:LinkButton ID="lbcancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                    Text="取消"></asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="lbedit" runat="server" CausesValidation="False" CommandName="Edit"
                                    Text="编辑"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        <span style="color: #ff0000">还没有数据!</span>
                    </EmptyDataTemplate>
                </asp:GridView>
                <br />
            </div>
        </form>

    这样以上的GridView编辑事件后的效果就出来了。接下来就应该是第二个表单  由于里面有一个服务器控件 所以此表单也应该有runat属性,如果是这样的话就发生了错误 违背了页面标准。有人提到使用js来触发按钮的点击事件  前台代码为:

         <!--隐藏的iframe来接受表单提交的信息-->
        <iframe name="ajaxifr" style="display: none;" id="ajaxifr"></iframe>
        <!--这里设置target="ajaxifr",这样表单就提交到iframe里面了,和平时未设置target属性时默认提交到当前页面-->
        <!--注意一点的是使用iframe时在提交到的页面可以直接输出js来操作父页面的信息,一般的ajax提交文本信息时你需要返回信息,如果是js信息你还得eval下-->
         <form id="form2" enctype="multipart/form-data" method="post" target="ajaxifr" action="banerman.aspx">


    <table border="0" cellspacing="2" style="text-align: left; width: 785px;" cellpadding="2"
                    bgcolor="#F3F2F3">
                    <tr>
                        <td style="text-align: left; width: 6%;">
                            名称</td>
                        <td style="width: 563px">
                            <input type="text" name="name" id="name" style="width: 132px;" runat="server" onblur="nameResult();" />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: center; width: 6%;">
                        </td>
                        <td style="width: 563px" id="add">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width: 6%;">
                            图片</td>
                        <td style="width: 563px">
                            <div id="preview_fake" style="width: 80px; height: 85px; border: solid 1px #99BBE8;"
                                runat="server">
                                <img id="preview" style="width: 99px; height: 106px; border: 0px;" />
                            </div>
                            <input type="file" id="file" name="file" runat="server" onchange="perImg(this)" />&nbsp;
                            <span id="ajaxMsg" class="alert">大小不能超过1M</span>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 6%">
                        </td>
                        <td style="width: 563px">
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 6%">
                            &nbsp;</td>
                        <td style="width: 563px">
                           <input onclick='megth();' type="submit" name="btn1" id="btn1" value="增加" width="70px"
                                runat="server" onserverclick="btn1_ServerClick" />
                        </td>
                    </tr>
                </table>      
        </form>
      <script type="text/javascript">
        function megth()
        {
           var btn=document.getElementById("btn1");
          btn.click();
        }
       
       
        </script>

     

    后台代码为:

    protected void btn1_ServerClick(object sender, EventArgs e)
        {
            string name = Request.Form["name"].ToString().Trim();//名称
            string path = string.Empty;
            HttpPostedFile f = Request.Files["file"];//获取上传的文件
            if (f.FileName != "")
            {
                //string oldfile = f.FileName.Substring(f.FileName.LastIndexOf("\\") + 1);
                string extension = Path.GetExtension(f.FileName).ToLower();
                int len = f.ContentLength;
                string newFileName = Guid.NewGuid().ToString() + extension;//使用guid生成新文件名
                if (len > 1 * 1024 * 1024)
                {

                    Response.Write("<script>window.parent.document.getElementById('ajaxMsg').className='error';</script>");
                    Response.Write("<script>window.parent.document.getElementById('ajaxMsg').innerHTML='<font color=#FF0000>图片超过1M,请重新上传</font>';</script>");
                    Response.Write("<script>window.parent.ClearFile();</script>");
                    Response.End();
                    return;
                }
                else
                {
                    f.SaveAs(Server.MapPath("~/banner/") + newFileName);
                    path = "banner/" + newFileName;
                }


                try
                {
                    using (SqlConnection con = OperateDB.GetConnection())
                    {
                        con.Open();
                        SqlTransaction st = con.BeginTransaction();
                        SqlParameter[] sp = new SqlParameter[2];
                        sp[0] = new SqlParameter("@name", name);
                        sp[1] = new SqlParameter("@path", path);

                        string str = OperateDB.ExecuteNonQueryWithTrans(con, st, "proc_banermanadd", sp);
                        if (str != "")
                        {
                            st.Rollback();
                            con.Close();
                            Response.Write("<script>alert('操作失败,请重新再试!');</script>");
                            return;
                        }
                        else
                        {
                            st.Commit();
                            Response.Write("<script>alert('操作成功!');window.parent.location.href='banerman.aspx'</script>");
                        }
                    }
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);//抛出异常
                }
            }
        }

    运行后同样错误! 未将对象引用设置到对象的实例。 反正经过了很多调试  有的时候执行以后当前页面变成了空白页。我所思右想终于让我想出了完美的解决方法,记得以前使用过多框架来解决页面中不能同时拥有两个带runat属性的表单的问题。于是乎就开始修改!就应该添加界面重新创建一个页面为baneradd.asp 与之前的页面banerman.aspx 再加上框架解决  全部代码如下:

     

    banerman.aspx代码如下:

    <body>
        <form id="form1" runat="server">
            <div style="margin-top: 30px;">
                <asp:GridView ID="GridView1" AutoGenerateColumns="False" Width="785px" runat="server"
                    OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                    OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:TemplateField HeaderText="编号">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server"><%#   Container.DataItemIndex   +   1%></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="30px" HorizontalAlign="Center" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="name" HeaderText="名称 ">
                            <ItemStyle Width="160px" />
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="图片地址">
                            <EditItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"path")%>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"path")%>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <a href='<%# ResolveUrl("~/"+Eval("path").ToString())%>' target="_blank">
                                    <img src='<%# ResolveUrl("~/"+Eval("path").ToString()) %>' alt='<%# Eval("name") %>'
                                        style="width: 30px; height: 40px; border: 0px;" /></a>
                            </ItemTemplate>
                            <%--<EditItemTemplate>
                                <div id="preview_fake1" style="width: 80px; height: 85px; border: solid 1px #99BBE8;"
                                    runat="server">
                                    <img id="preview1" style="width: 99px; height: 106px; border: 0px;" />
                                </div>
                                <input type="file" id="file1" name="file1" runat="server" onchange="perImg(this)" />&nbsp;
                                <span id="ajaxMsg" class="alert">大小不能超过1M</span>
                            </EditItemTemplate>--%>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="lbdelete" runat="server" CausesValidation="False" CommandName="Delete"
                                    Text="删除"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:LinkButton ID="lbupdate" runat="server" CausesValidation="True" CommandName="Update"
                                    Text="更新"></asp:LinkButton>
                                <asp:LinkButton ID="lbcancel" runat="server" CausesValidation="False" CommandName="Cancel"
                                    Text="取消"></asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="lbedit" runat="server" CausesValidation="False" CommandName="Edit"
                                    Text="编辑"></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle Width="50px" HorizontalAlign="Center" />
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                        <span style="color: #ff0000">还没有数据!</span>
                    </EmptyDataTemplate>
                </asp:GridView>
                <br />
            </div>
        </form>
        <iframe id="modfrm" name="modfrm" style="width: 800px; height: 800px; margin-left: 10px;
            margin-top: 30px; text-align: left" frameborder="0" src="baneradd.aspx"></iframe>
    </body>

     

    baneradd.aspx 代码如下:

    <body>
        <!--隐藏的iframe来接受表单提交的信息-->
        <iframe name="ajaxifr" style="display: none;" id="ajaxifr"></iframe>
        <!--这里设置target="ajaxifr",这样表单就提交到iframe里面了,和平时未设置target属性时默认提交到当前页面-->
        <!--注意一点的是使用iframe时在提交到的页面可以直接输出js来操作父页面的信息,一般的ajax提交文本信息时你需要返回信息,如果是js信息你还得eval下-->
        <form id="form1" runat="server" enctype="multipart/form-data" method="post" target="ajaxifr">
            <div>
                <table border="0" cellspacing="2" style="text-align: left; width: 785px;" cellpadding="2"
                    bgcolor="#F3F2F3">
                    <tr>
                        <td style="text-align: left; width: 6%;">
                            名称</td>
                        <td style="width: 563px">
                            <input type="text" name="name" id="name" style="width: 132px;" runat="server" onblur="nameResult();" />
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: center; width: 6%;">
                        </td>
                        <td style="width: 563px" id="add">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width: 6%;">
                            图片</td>
                        <td style="width: 563px">
                            <div id="preview_fake" style="width: 80px; height: 85px; border: solid 1px #99BBE8;"
                                runat="server">
                                <img id="preview" style="width: 99px; height: 106px; border: 0px;" />
                            </div>
                            <input type="file" id="file" name="file" runat="server" onchange="perImg(this)" />&nbsp;
                            <span id="ajaxMsg" class="alert">大小不能超过1M</span>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 6%">
                        </td>
                        <td style="width: 563px">
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 6%">
                            &nbsp;</td>
                        <td style="width: 563px">

                          <input type="submit" name="btn1" id="btn1" value="增加" width="70px"
                                runat="server" onserverclick="btn1_ServerClick"  onclick="return Check(this)" />
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>

     

    至此问题全部解决!感谢大家的帮忙!谢谢!

     


    煜诚海参专卖 淘宝店铺:http://jinweb.taobao.com
    • 已标记为答案 jinwb1982 2011年3月7日 9:07
    2011年3月7日 9:07