我有一个gridview,我希望能够选择并获取所选行的第2列中的单元格的值。 我的gridview如下:
<asp:GridView ID="gridviewSLds" runat="server" CellPadding="0" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="False" OnRowCreated="gridviewSLds_RowCreated">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
<Columns>
<asp:TemplateField ItemStyle-BorderWidth="0">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="item" HeaderText="Metric" SortExpression="item" ReadOnly="false" />
<asp:TemplateField HeaderText="Item">
<ItemTemplate>
<asp:TextBox onfocusin="select()" runat="server" Text='<%# Bind("item") %>'
ID="txtfocus" class="modalpopup" SelectedRowStyle="myselection" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Control Type">
<ItemTemplate>
<asp:TextBox onfocusin="select()" runat="server" Text='<%# Bind("itemCtrlType") %>'
ID="txtfocus2" class="modalpopup2" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
我可以使用以下代码获取行索引:
<script>
$(document).ready(function () {
$('.modalpopup').focus(function () {
var iIndex = $(this).closest("tr").prevAll("tr").length;
});
});
如何使用它来获取第2列中的单元格值和iIndex给出的行?
答案 0 :(得分:0)
这将有效:
<script type="text/javascript">
$(document).ready(function () {
$('.modalpopup').focus(function () {
var idString = this.id.replace("txtfocus", "txtfocus2");
var rowNumber = this.id.split("_")[3];
alert($("#" + idString).val() + " - " + rowNumber);
});
});
</script>
您只需要确保拆分功能正确。检查gridview中文本框的ID(应该看起来像这样ContentPlaceHolder1_gridviewSLds_txtfocus_14
)