使用带有asp.net的jquery双击Datagrid视图

时间:2011-02-28 18:58:21

标签: jquery asp.net

我在asp.net页面中有一个名为gvLineItems的gridView .. 我想抛出一些jquery以允许双击gridview的行并让它重定向到另一个页面:

<script src="include/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

var selected = null;

$(document).ready(function(){
   $("#divGridView table tbody tr").find("tr").dblclick(function(){
      window.location = "<%#ResolveUrl("~/Default2.aspx")%>?record=" + $(this).find("td:first").text();
   });
});

</script>

有人在这看到任何奇怪的事吗?

这是真正丑陋的asp.net ajax与div等...

<div id="divGridView" style="width:100%">
                                <asp:UpdatePanel ID="myPanel" runat="server" >
                                          <ContentTemplate>
                                        <asp:GridView GridLines="Horizontal" CellPadding="4" Font-Size="Small" DataKeyNames="ID" Width="100%" AlternatingRowStyle-BackColor="#F0F8FF" BackColor="#E8E8E8" HeaderStyle-BackColor="#377CB1" ID="gvLineItems" runat="server" AllowSorting="True" AutoGenerateColumns="False" ShowFooter="True" >
                                            <Columns>

                                                  <asp:TemplateField HeaderText="ID" SortExpression="ID" Visible="False">
                                                     <ItemTemplate>
                                                         <asp:Label ID="lblExpenseReportLineItemID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>'></asp:Label>
                                                     </ItemTemplate>
                                                  </asp:TemplateField>

                                                  <asp:TemplateField>
                                                    <HeaderTemplate>
                                                        <asp:CheckBox ID="HeaderLevelCheckBox" AutoPostBack="true" OnCheckedChanged="SelectAllRows" runat="server" />
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="chkSelector" runat="server" onclick="ChangeRowColor(this)" />
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Center" Width="1%" />
                                                    <HeaderStyle HorizontalAlign="Center" Width="1%" />
                                                  </asp:TemplateField>   

                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:ImageButton ID="ibAddLineItem" runat="server" ImageUrl="images/InsertRow.gif"   CommandName="Insert" ToolTip="Insert line item" />
                                                        </ItemTemplate>
                                                        <ItemStyle Width="1%" />
                                                    </asp:TemplateField>

                                                     <asp:TemplateField>
                                                <ItemTemplate>

                                                        <asp:ImageButton ID="ibDelete" runat="server" CommandName="DeleteRow" ImageUrl="images/d.gif"
                                                            ToolTip="Delete Line Item?" />

                                                </ItemTemplate>
                                            </asp:TemplateField>

                                                    <asp:TemplateField HeaderText="Item Date" SortExpression="LineItemDate">
                                                        <ItemTemplate>
                                                            <ajaxToolkit:CalendarExtender TargetControlID="txtLineItemDate" ID="CalendarExtender3" runat="server">
                                                            </ajaxToolkit:CalendarExtender>
                                                            <asp:TextBox  Width="60px" onchange='return isLessThanEndDate(this);' ID="txtLineItemDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemDate") %>'>
                                                            </asp:TextBox>
                                                        </ItemTemplate>
                                                        <ItemStyle HorizontalAlign="Center" Width="10%" />
                                                        <HeaderStyle ForeColor="White" HorizontalAlign="Center" Width="12%" />
                                                    </asp:TemplateField>             

                                                    <asp:TemplateField HeaderText="Comm.CU" SortExpression="Comm">
                                                        <ItemTemplate>
                                                            <table width="100%" cellpadding="2" cellspacing="0" style=" border-style:solid; border-width:thin; border-color:#DDD;">
                                                                <tr>
                                                                <td ><asp:TextBox Enabled="False" ToolTip="Click Search...To find a commission" Width="40px" ID="txtComm" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CommissionID") %>' /></td>
                                                                </tr>
                                                                <tr><td><asp:TextBox Enabled="False" ToolTip="Click Search...To find a cost unit" Width="40px" ID="txtWBS" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.WBSID") %>' /></td></tr>
                                                                <tr><td align="center"><a href='FindCommCost.aspx?ExpenseReportID=<%#Eval("ERID")%>&ExpenseReportLineItemID=<%#Eval("ID")%>'  style="color:Navy">Search...</a></td></tr>
                                                            </table>



                                                        </ItemTemplate>
                                                        <ItemStyle Width="1%" />
                                                    </asp:TemplateField>


                                                    <asp:TemplateField HeaderText="Type" SortExpression="Reason">
                                                        <ItemTemplate>
                                                            <asp:DropDownList ID="ddlExpenseTypes" OnSelectedIndexChanged="ddlExpenseTypes_SelectedIndexChanged"  DataSource='<%# GetExpenseTypes() %>' SelectedValue='<%# Bind("ExpenseReasonID") %>' DataTextField="ExpenseReasonID" DataValueField="ExpenseReasonID" AutoPostBack="true"  runat="server" ></asp:DropDownList>
                                                        </ItemTemplate>
                                                        <ItemStyle HorizontalAlign="Center" Width="1%" />
                                                    </asp:TemplateField>  

                                                    <asp:TemplateField HeaderText="Receipt" SortExpression="Date">
                                                        <ItemTemplate>
                                                            <asp:CheckBox ID="cbReceipt" runat="server" Checked='<%# DataBinder.Eval(Container, "DataItem.HasReceipt") %>' />
                                                        </ItemTemplate>
                                                        <HeaderStyle ForeColor="White" />
                                                        <ItemStyle HorizontalAlign="Center" Width="1%" />
                                                    </asp:TemplateField>             

                                                    <asp:TemplateField HeaderText="Amount" SortExpression="Amount">
                                                        <ItemTemplate>
                                                            <asp:TextBox Width="40px" AutoPostBack="true" OnTextChanged="ChangedAmount" ID="txtAmount" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AmountSpent") %>' />
                                                        </ItemTemplate>
                                                        <ItemStyle Width="1%" />
                                                    </asp:TemplateField>

                                                     <asp:TemplateField HeaderText="Currency" SortExpression="Currency">
                                                        <ItemTemplate>
                                                            <asp:DropDownList ID="ddlCurrency" OnSelectedIndexChanged="ddlCurrency_SelectedIndexChanged" runat="server" AutoPostBack="true" DataSource='<%# GetCurrency() %>' SelectedValue='<%# Bind("CountryID") %>' DataTextField="CountryID" DataValueField="CountryID"></asp:DropDownList>
                                                        </ItemTemplate>
                                                        <ItemStyle HorizontalAlign="Center" Width="1%" />
                                                    </asp:TemplateField>  

                                                    <asp:TemplateField HeaderText="Rate" SortExpression="Rate">
                                                        <ItemTemplate>
                                                            <asp:TextBox Width="33px" AutoPostBack="true" ID="txtRate" OnTextChanged="ChangedAmount" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Rate") %>' /> 
                                                        </ItemTemplate>
                                                        <ItemStyle Width="1%" />
                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText="USD" SortExpression="USD">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblUSD" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.AmountBackInUSD") %>' /> 
                                                        </ItemTemplate>
                                                        <ItemStyle Width="1%" />
                                                    </asp:TemplateField>

                                                    <asp:TemplateField HeaderText="Desc." SortExpression="Desc.">
                                                        <ItemTemplate>
                                                            <asp:TextBox Width="100px" Font-Names="Arial" ToolTip="If the expense type is Entertainment this field is required!" ID="txtExpenseReasonDescription" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExpenseReasonDescription") %>' /> 
                                                        </ItemTemplate>
                                                        <ItemStyle Width="1%" HorizontalAlign="Left" />
                                                    </asp:TemplateField>


                                                     <asp:HyperLinkField HeaderText="Link" SortExpression="Link" DataNavigateUrlFields="AttachmentLink" DataNavigateUrlFormatString="{0}"
                                                DataTextField="AttachmentLink" DataTextFormatString="Link" Target="_blank" >
                                                         <ControlStyle ForeColor="Navy" />
                                                         <HeaderStyle ForeColor="White" />
                                                     </asp:HyperLinkField>

                                                    <asp:TemplateField HeaderText="Upload File?" SortExpression="ID" >
                                                    <ItemTemplate>
                                                        <asp:FileUpload Width="90px" Font-Size="xx-small"  ID="fuAttachment" runat="server" />
                                                        <asp:Button ID="btnUpload" font-size="xx-small" runat="server" Text="Upload" OnClick="UploadTheFile" />
                                                    </ItemTemplate>
                                                        <HeaderStyle ForeColor="White" />
                                                        <ItemStyle Width="50%" />
                                                    </asp:TemplateField>
                                            </Columns>
                                            <HeaderStyle BackColor="#377CB1" />
                                            <AlternatingRowStyle BackColor="AliceBlue" />
                                            <FooterStyle CssClass="FooterStyle" />
                                        </asp:GridView>
                                        <div style="width:100%">
                                            <br />
                                    <asp:UpdateProgress ID="udProgress" runat="server" DisplayAfter="100" Visible="True" DynamicLayout="True"><ProgressTemplate><img border="0" src="images/loading.gif" alt="loading" /></ProgressTemplate></asp:UpdateProgress>
                                            <br />
                                            <asp:Label ID="lblMessage2" runat="server"></asp:Label>
                                        </div>
                                        <div style="width:100%; text-align:right;">
                                        <table width="100%" cellpadding="2" cellspacing="0">
                                        <tr>
                                        <td class="tableFields" style="height: 68px">
                                            &nbsp;</td>
                                        <td align="right">
                                        <asp:Panel ID="pGridControls" runat="server" Width="100%" Visible="False">
                                            <asp:Label ID="Label2" runat="server" Text="Click Update to <u>save</u> any changes" Font-Bold="True" ForeColor="#377CB1"></asp:Label>
                                        <asp:ImageButton ID="ibSaveGrid" runat="server" ImageUrl="images/smallSave.gif" ToolTip="Save / refresh changes?" CausesValidation="False" OnClick="ibSaveGrid_Click" /><asp:ImageButton ID="ibDeleteGrid" runat="server" ImageUrl="images/smallDelete.gif" ToolTip="Delete checked rows?" CausesValidation="False" OnClick="ibDeleteGrid_Click" /></asp:Panel>
                                            <hr /> <h3>
                                                <asp:Panel ID="Panel5" runat="server" Width="50%" HorizontalAlign="Right" BackColor= "#F0FFF0" CssClass="PadIt">
                                                    <ajaxToolkit:RoundedCornersExtender ID="RoundedCornersExtender4" runat="server" TargetControlID="Panel5" Radius="6" Corners="All" BorderColor="#377CB1" Color="#F0FFF0">
                                                    </ajaxToolkit:RoundedCornersExtender>
                                                Employee Due (USD):<asp:Label ID="lblMessage3" runat="server"></asp:Label>
                                                </asp:Panel>
                                                </h3> </td>
                                        </tr>
                                        </table>
                                        </div>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ibOk" />
                                        <asp:AsyncPostBackTrigger ControlID="rbSort" />
                                    </Triggers>
                                </asp:UpdatePanel>
                                    <asp:Literal ID="CheckBoxIDsArray" runat="server"></asp:Literal></div>
                                </td>
                            </tr>
                        </table>
                            </asp:Panel>
                    </div>

这是我的编辑:

<script type="text/javascript">

var selected = null;

$(document).ready(function(){
   $("#divGridView table tbody tr").dblclick(function(){
      window.location = "<%#ResolveUrl("~/Default2.aspx")%>?ID=" + $(this).find("td:first").text();
   });
});

</script>

我现在几乎已经开始工作......我可以看到正确的ID号,可以解决为什么重定向不起作用。

3 个答案:

答案 0 :(得分:1)

将gridview包含在<div id="divGridView" />中并尝试此

$(document).ready(function(){
   $("#divGridView table tbody tr").dblclick(function(){
    var id= $(this).find("td:first").text();
      window.location = "<%#ResolveUrl("~/Default2.aspx")%>?record=" + id;
   });
});

答案 1 :(得分:1)

您正在寻找获取ID的标记,$(this).find("td:first").text()Visible=false,这会导致它无法呈现,因此jQuery无法找到它。 最简单的解决方案可能是将ID放入标记(如span)中,该标记仅在第一个显示列中使用display:none呈现为不可见的客户端

答案 2 :(得分:0)

这是我最终做的事情:

<script type="text/javascript">

var selected = null;

$(document).ready(function(){
   $("#divGridView table tbody tr").dblclick(function(){
      //window.location = "<%#ResolveUrl("~/Search.aspx")%>?ID=" + $(this).find("td:first").text();
      window.location = "myURL/Search.aspx?ID=" + $(this).find("td:first").text();
   });
});

</script>