2012-10-14 42 views
2

我已經實現了哪些功能,當用戶在GridView頁腳中並按下Enter鍵時該行將被插入。在GridView中我有2 <asp:ImageButton>一個在<EmptyDataTemplate>而另一個在<FooterTemplate>我寫了JavaScript來執行ImageButton Click事件,當使用在最後一個字段時,ImageButton服務器端點擊事件被觸發但頁面沒有更新。引發ImageButton服務器端事件,但頁面沒有使用ASP.NET UpdatePanel更新

這裏的javascrip功能:

function insertByEnterKey(buttonId) { 
     var button = document.getElementById(buttonId); 
     var keyEvent = event.keyCode; 

     if (keyEvent == 13) { 
      button.click(); 

     } 
    } 

下面是ASPX:

<asp:UpdatePanel ID="UpdatePanel" runat="server"> 
     <ContentTemplate> 
      ... 
      <asp:GridView ID="grvDonationDist" runat="server" AutoGenerateColumns="False" ShowFooter="True" 
       DataKeyNames="reciept_num,donation_code" meta:resourcekey="grvDonationDistResource1" 
       ShowHeaderWhenEmpty="True" BorderWidth="1px" BackColor="White" BorderColor="LightSteelBlue" 
       CellPadding="0" Font-Name="tahoma" Font-Size="10pt" ForeColor="DarkBlue" HeaderStyle-BackColor="#aaaadd" 
       GridLines="None"> 
       <EmptyDataTemplate> 
        <asp:Label CssClass="label" ID="lblEmptyDonationDist" runat="server" Text="No Donations" 
         meta:resourcekey="lblEmptyDonationDistResource1"></asp:Label> 
        <tr> 
         <td> 
          <asp:ImageButton ID="lbtnAdd" runat="server" OnClick="lbtnAdd_Click" meta:resourcekey="AddResource1" 
           ImageUrl="Content/images/add.png"></asp:ImageButton> 
         </td> 
         ... 
         <td> 
          <asp:TextBox ID="txtDonationNotesFooter" CssClass="textbox" runat="server" meta:resourcekey="txtDonationNotesResource1" 
           onKeyDown="insertByEnterKey('lbtnAdd');"></asp:TextBox> 
         </td> 
        </tr> 
       </EmptyDataTemplate> 
       <Columns> 
        <asp:TemplateField FooterText="Total" ShowHeader="False"> 
         <EditItemTemplate> 
          ... 
         </EditItemTemplate> 
         <ItemTemplate> 
          ... 
         </ItemTemplate> 
         <FooterTemplate> 
          <asp:ImageButton ID="lbtnAdd" runat="server" OnClick="lbtnAddFromFooter_Click" meta:resourcekey="AddResource1" 
           ImageUrl="Content/images/add.png"></asp:ImageButton> 
         </FooterTemplate> 
        </asp:TemplateField> 
        ... 
        <asp:TemplateField HeaderText="Notes" SortExpression="distribution_remrks" meta:resourcekey="TemplateFieldResource3"> 
         <EditItemTemplate> 
          ... 
         </EditItemTemplate> 
         <ItemTemplate> 
          ... 
         </ItemTemplate> 
         <FooterTemplate> 
          <asp:TextBox ID="txtDonationNotesFooter" CssClass="textbox" runat="server" meta:resourcekey="txtDonationNotesResource1" 
           onKeyDown="insertByEnterKey('lbtnAdd');"></asp:TextBox> 
         </FooterTemplate> 
        </asp:TemplateField>     
       </Columns> 
      </asp:GridView> 
     </ContentTemplate> 
</asp:UpdatePanel> 

這裏是VB.NET:

Protected Sub lbtnAddFromFooter_Click(sender As Object, e As EventArgs) 
    'Footer Add 
    'Add Insert Logic here 
    Try 

     Dim donationDist As New DonationDist 

     Dim txtDonationValueFooter As TextBox = grvDonationDist.FooterRow.FindControl("txtDonationValueFooter") 
     Dim txtDonationNotesFooter As TextBox = grvDonationDist.FooterRow.FindControl("txtDonationNotesFooter") 
     Dim ddlCountryFooter As DropDownList = grvDonationDist.FooterRow.FindControl("ddlCountryFooter") 
     Dim ddlPurposeFooter As DropDownList = grvDonationDist.FooterRow.FindControl("ddlNewDonationPurposeType") 
     Dim chkPartial As CheckBox = grvDonationDist.FooterRow.FindControl("chkPartialFooter") 
     Dim standInstruct As Label = grvDonationDist.FooterRow.FindControl("lblStandInstructFooter") 
     Dim donationValue As Decimal = Convert.ToDecimal(txtDonationValueFooter.Text) 

     'Validation: Donation Value must be > 0 
     If (donationValue <= 0) Then 

      If (CultureInfo.CurrentUICulture.Name.Contains("ar")) Then 

       ShowAlert("قيمة الترع يجب ان تكون أكبر من الصفر") 

      ElseIf (CultureInfo.CurrentUICulture.Name.Contains("en")) Then 

       ShowAlert("Donation Value must be greater than 0") 

      End If 

      Exit Sub 

     End If 

     myDonationDistDataTable = Session("myDonationDistDataTable") 

     'Validation: Only one donation type per Receipt 
     For Each row As DataRow In myDonationDistDataTable.Rows 

      If (row.Item("donation_code") = ddlPurposeFooter.SelectedValue) Then 

       If (CultureInfo.CurrentUICulture.Name.Contains("ar")) Then 

        ShowAlert("لا يمكن تكرار الغرض في نفس سند القبض") 

       ElseIf (CultureInfo.CurrentUICulture.Name.Contains("en")) Then 

        ShowAlert("You cannot add more than on Donation Type per receipt") 

       End If 

       Exit Sub 
      End If 

     Next 



     myDonationDistDataRow = myDonationDistDataTable.NewRow() 
     myDonationDistDataRow("reciept_num") = 0 
     myDonationDistDataRow("donation_code") = Convert.ToInt16(ddlPurposeFooter.SelectedValue) 
     myDonationDistDataRow("donation_name") = ddlPurposeFooter.SelectedItem.Text 
     myDonationDistDataRow("donation_value") = Convert.ToDecimal(txtDonationValueFooter.Text) 
     myDonationDistDataRow("country_code") = Convert.ToInt16(ddlCountryFooter.SelectedValue) 
     myDonationDistDataRow("country_name") = ddlCountryFooter.SelectedItem.Text 
     myDonationDistDataRow("distribution_remrks") = txtDonationNotesFooter.Text 
     myDonationDistDataRow("partial") = chkPartial.Checked 
     myDonationDistDataRow("standing_inst_num") = If(String.IsNullOrWhiteSpace(standInstruct.Text), 0, Convert.ToInt32(standInstruct.Text)) 

     'add the new DataRow to DataTable's Row 
     myDonationDistDataTable.Rows.Add(myDonationDistDataRow) 

     Session("myDonationDistDataTable") = myDonationDistDataTable 

     grvDonationDist.DataSource = myDonationDistDataTable 
     grvDonationDist.DataBind() 

    Catch ex As Exception 
     'TODO: Log the exception 
    End Try 
End Sub 

什麼是錯的代碼?我錯過了什麼嗎?

+0

你能後的'lbtnAddFromFooter_Click'方法的代碼? –

+0

我已經添加了方法@NickW –

+0

這是'Catch'塊得到一個異常?如果是這樣,如果您可以發佈堆棧跟蹤,這將是一件好事。 –

回答

0

我解決它只是通過添加insertByEnterKey()功能

onKeyDown="insertByEnterKey('lbtnAdd'); return false;" 

更好的解決方案後,返回false:

使JavaScript函數返回False當用戶按下「Enter Key」時,否則返回True。

function insertByEnterKey(buttonId) { 
      var button = document.getElementById(buttonId); 
      var keyEvent = event.keyCode; 

      if (keyEvent == 13) { 
       button.click(); 
       return false; 
      } else { 
       return true; 
      } 
     } 

,並在文本框像這樣使用它:

<asp:TextBox ID="txtDonationNotesFooter" runat="server" onKeyDown="return insertByEnterKey('lbtnAdd');"></asp:TextBox> 
0

你不能這樣做的,你需要將事件添加到更新面板
的javascript:

function insertByEnterKey(buttonId) { 
     var button = document.getElementById(buttonId); 
     var keyEvent = event.keyCode; 

     if (keyEvent == 13) { 
      __doPostBack('<%= UpdatePanel.UniqueId %>', ''); 
     } 
    } 

ASPX:

<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional" onload="UpdatePanel_Load"> 
    <ContentTemplate> 

    </ContentTemplate> 
</asp:UpdatePanel> 

CS:

protected void UpdatePanel_Load(object sender, EventArgs e) 
    { 
     //add your code here 
    } 

如果您沒有在代碼中獲得_doPostBack你可以這樣加吧..

protected void Page_Load(object sender, EventArgs e) 
    { 
     ClientScript.GetPostBackEventReference(this, string.Empty); 
    } 
+0

我不想刷新UpdatePanel我只想觸發服務器端ImageButton_Click事件並刷新UpdatePanel –

相關問題