2013-09-27 47 views
0

我有一个可扩展的GridView,带有一个TextBox和一个Button的TemplateField,其中的想法是,当按钮被按下时,它将保存文本框的内容进入数据库。目前我没有将TextBox的内容加载到数据库中,因为我在测试时遇到了问题。在Gridview中嵌套的Ajax UpdatePanel需要按钮控件被点击两次

我在Ajax UpdatePanel里面有Gridview,在Ajax UpdatePanel里面也有TemplateField里面的TexBox和Button。

问题是,当gridview从触发dropdownfield的更改更新时,我需要按两次按钮才能得到我想要的结果(目前只需在该行输入ID) 。

继另一篇文章(Why does a button control need to be clicked twice?)之后,我尽了最大努力来包含嵌套UpdatePannel中的所有信息。但我仍然无法摆脱这个问题。我猜这个问题是由于试图在行中查找TextBox控件以便我可以编辑文本,但我无法弄清楚如何避免使用FindControl。

请参见下面的代码:

<script language="javascript" type="text/javascript"> 
    function divexpandcollapse(divname) {var div = document.getElementById(divname); 
     var img = document.getElementById('img' + divname); 
     if (div.style.display == "none") { 
      div.style.display = "block"; img.src = "Images/Icons/minus.jpg"; 
     } else { div.style.display = "none"; img.src = "Images/Icons/plus.jpg"; } 
    }</script> 

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ScriptMode="Release" 
    CompositeScript-ScriptMode="Release" AllowCustomErrorsRedirect="False" EnablePartialRendering="true"> 
</asp:ToolkitScriptManager> 

<asp:UpdatePanel ID="UP_TabContainer" runat="server" UpdateMode="Conditional"> 
<ContentTemplate> 
<asp:DropDownList ID="DateSelection" runat="server" AutoPostBack="True"> </asp:DropDownList> 
<asp:GridView ID="GV_SL" runat="server" OnRowDataBound="gvUserInfo_RowDataBound" 
    OnRowCommand="GV_SL_RowCommand" AutoGenerateColumns="False" DataSourceID="SQL_Weekly"> 
    <Columns> 

    <asp:TemplateField ItemStyle-Width="50px"> 
    <ItemTemplate> 
     <a href="JavaScript:divexpandcollapse('div<%# Eval("reporting_group") %>');"> 
     <img id="imgdiv<%# Eval("reporting_group") %>" width="15px" border="0" src="Images/Icons/plus.jpg" /></a> 
    </ItemTemplate> 
    <ItemStyle Width="40px" /> 
    </asp:TemplateField> 

    <asp:BoundField DataField="name" HeaderText="Group" SortExpression="name" /> 
    <asp:BoundField DataField="reporting_group" HeaderText="ID" ReadOnly="True" SortExpression="reporting_group" /> 

    <asp:TemplateField> 
    <ItemTemplate> 
     <tr> 
     <td colspan="100%"> 
     <div id="div<%# Eval("reporting_group") %>" style="display: none; position: relative; 
     left: 15px; overflow: auto"> 
      <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false"> 
      <Columns> 
      <asp:BoundField DataField="Metric" HeaderText=" " HeaderStyle-HorizontalAlign="Left" /> 
      <asp:BoundField DataField="Actual" HeaderText="Actual" HeaderStyle-HorizontalAlign="Left" /> 
      </Columns> 
      </asp:GridView> 

      <asp:UpdatePanel ID="UP_Comments" runat="server" UpdateMode="Always"> 
      <ContentTemplate> 
      <asp:TextBox ID="TB_Comments" runat="server" Text="Example: Text will be entered here"></asp:TextBox> 
      <asp:Button ID="B_Save" runat="server" CommandName="AddText" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" 
       Text="Save Changes" /> 
      <asp:Label ID="Label1" runat="server" Text='<%# eval("reporting_group") %>' Visible="False"></asp:Label> 
      </ContentTemplate> 
      </asp:UpdatePanel> 
     </div> 
     </td> 
     </tr> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
    </asp:GridView> 
    </ContentTemplate> 
    <Triggers> 
    <asp:AsyncPostBackTrigger ControlID="DateSelection" EventName="SelectedIndexChanged" /> 
    </Triggers> 
</asp:UpdatePanel> 

VB的按钮后面的代码

Protected Sub GV_SL_RowCommand(ByVal sender As Object, _ 
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) 
    If (e.CommandName = "AddText") Then 
     ' Retrieve the row index stored in the CommandArgument property. 
     Dim index As Integer = Convert.ToInt32(e.CommandArgument) 
     ' Retrieve the row that contains the button 
     ' from the Rows collection. 
     Dim row As GridViewRow = GV_SL.Rows(index) 
     Dim LB_RG As System.Web.UI.WebControls.Label = DirectCast(row.FindControl("Label1"), System.Web.UI.WebControls.Label) 
     Dim TB_Com_Control As System.Web.UI.WebControls.TextBox = DirectCast(row.FindControl("TB_Comments"), System.Web.UI.WebControls.TextBox) 
     TB_Com_Control.Text = "Test " & "-" & LB_RG.Text 
    End If 
End Sub 

我希望有人可以帮助我,试图找出问题的所在和如何解决它。

回答

0

我已成功地解决由缠绕的UpdatePanel ID = “UP_TabContainer” 内的以下控制问题:

  • GridView的ID = “GV_SL”
  • 的DataSourceID = “SQL_Weekly”

并排除:

  • function divexpandcollapse(divname)
  • ToolkitScriptManager ID = “ToolkitScriptManager1”
  • 的DropDownList ID = “DateSelection”

我不知道为什么它的工作原理,但我希望它可以帮助别人。

相关问题