0

我有一个gridview,默认情况下所有行都是可编辑的。大多数列都需要一个简单的文本框,其中包含一些格式验证器,这不是问题。

但是,我确实有 1 列需要用户可以从中选择的选项列表。为此,我使用 Ajax Drop Down Extender 绑定到文本框,因此当单击文本框时,它会为它们提供列表...足够简单。

用户从下拉列表中选择一个选项后出现问题,我似乎无法让文本框用新选择的选项更新它的值。

这是来自 gridview 列的 ItemTemplate。

<%--  PRIORITY --%> 
                <asp:TemplateField HeaderText="PRI" SortExpression="PRIORITY">
                    <ItemTemplate>                                                                   
                        <ItemStyle CssClass="ssCellSelected" />
                        <asp:Panel ID="priorityitems" runat="server" BorderColor="Aqua" BackColor="White" BorderWidth="1">
                        <asp:ListBox ID="lstPRIORITY" runat="server" SelectedItem='<%# Bind("PRIORITY") %>'>
                            <asp:ListItem>P1</asp:ListItem>
                            <asp:ListItem>P2</asp:ListItem>
                            <asp:ListItem>P3</asp:ListItem>
                        </asp:ListBox>
                        </asp:Panel>
                        <asp:TextBox ID="PRIORITY" runat="server" Width="35px" Text='<%# Eval("PRIORITY") %>' CssClass="ssTextBox"></asp:TextBox>
                        <cc1:DropDownExtender ID="PRIORITY_DropDownExtender" runat="server" 
                            Enabled="True" DropDownControlID="priorityitems" TargetControlID="PRIORITY">
                        </cc1:DropDownExtender>
                    </ItemTemplate> 
                    <ItemStyle CssClass="ssCell" />                   
                </asp:TemplateField>

这是为每一行创建 onclick 事件的代码。

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then Dim lstPRI As ListBox = DirectCast(e.Row.Cells(3).FindControl("lstPRIORITY"), ListBox) Dim rowIndex As Integer = e.Row.RowIndex Dim columIndex As Integer = 3 '列索引为 0 If lstPRI IsNot Nothing Then lstPRI.Attributes.Add("onclick", "setText(this.options[this.selectedIndex].value," & rowIndex.ToString & "," & columIndex.ToString & ");") End If End If

End Sub

我需要使用 [this.selectedIndex].Value 并将其应用于 ID 为 PRIORITY 的 TextBox

我需要以某种方式把它变成它的动态对应物

<script type="text/javascript" language="javascript">
    function setText(newValue, row, column) {
        document.getElementById("ctl00_pagebody_GridView1_ctl02_PRIORITY").value = newValue;
    }
</script>
4

1 回答 1

0

我能够使用 Dom 和 Javascript 来完成这项工作。

在这里找到了一个很好的教程... http://www.opensourcetutorials.com/tutorials/Client-Side-Coding/JavaScript/javascript-document-object-model/page1.html

无论如何,我安装了 IE DEV TOOLS,这样我就可以查看 gridview 的 HTML 并找出文本框的位置......

然后在aspx页面上的脚本函数中..

请注意,我必须选择 gridview 的第一个子节点,因为表格内部是一个包含整个表格结构的标签。

我还必须将 1 添加到行整数,因为第一行是 gridview 表的标题

<script type="text/javascript" language="javascript">
    function setText(newValue, therow, column) {
        var gridView = document.getElementById('<%= GridView1.ClientID %>').childNodes[0];
        var row = gridView.childNodes[parseInt(therow) + 1];
        var cell = row.cells[column];
        cell.childNodes[1].childNodes[0].value = newValue;
    }       
</script> 
于 2010-01-11T21:52:17.280 回答