2

我在转发器中有一个AutoCompleteExtender AjaxControlToolkit 控件,需要从 Web 服务获取名称和值。自动完成字段需要存储名称,并且有一个隐藏字段需要存储值。当尝试在中继器之外执行此操作时,我通常会让事件OnClientItemSelected调用类似于的 javascript 函数

function GetItemId(source, eventArgs)
{
   document.getElementById('<%= ddItemId.ClientID %>').value = eventArgs.get_value();
}

但是,由于需要将值存储在转发器的控件中,我需要一些其他方式让 javascript 函数“获取”组件来存储值。

4

3 回答 3

1

I've got some JavaScript that might help you. My ASP.Net AutoComplete extender is not in a repeater, but I've modified that code to detect the ID of the TextBox you are going to write the erturned ID to, it should work (but I haven't tested it all the way through to post back). Use the value from 'source' parameter in the client side ItemSelected method. That is the ID of the calling AutoComplete extender. Just make sure that you assign an ID the hidden TextBox in the Repeater Item that is similar to the ID of the extender. Something like this:

<asp:Repeater ID="RepeaterCompareItems" runat="server">
    <ItemTemplate>
        <ajaxToolkit:AutoCompleteExtender runat="server" 
           ID="ACE_Item"
           TargetControlID="ACE_Item_Input"
           ...other properties...
           OnClientItemSelected="ACEUpdate_RepeaterItems" />
        <asp:TextBox ID="ACE_Item_Input" runat="server" />
        <asp:TextBox ID="ACE_Item_IDValue" runat="server" style="display: none;" />
    </ItemTemplate>
</asp:Repeater>

Then the JS method would look like this:

 function ACEUpdate_CustomerEmail(source, eventArgs) {
            UpdateTextBox = document.getElementById(source.get_id() + '_IDValue');
            //alert('debug = ' + UserIDTextBox);
            UpdateTextBox.value = eventArgs.get_value();
            //alert('customer id = ' + UpdateTextBox.value);
        }

There are extra alert method calls that you can uncomment for testing and remove for production. In a simple and incomplete test page, I got IDs that looked like this: RepeaterCompareItems_ctl06_ACE_Item_IDValue (for the text box to store the value) and RepeaterCompareItems_ctl07_ACE_Item (for the AC Extender) - yours may be a little different, but it looks practical. Good Luck.

于 2009-03-05T19:15:22.170 回答
1

如果我正确理解了这个问题,你应该能够做你通常做的事情,但不是嵌入 ClientId,而是使用“源”参数。这应该允许您访问要更新的控件。

于 2009-03-23T11:05:16.297 回答
0

由于您使用的是中继器,我建议连接 OnItemDataBound 函数...

<asp:Repeater id="rptResults" OnItemDataBound="FormatResults" runat="server">

<项目模板>

<asp:PlaceHolder id="phResults" runat="server" />

</ItemTemplate>

</asp:中继器>

然后在后面的代码中使用类似

`Private Sub FormatResults(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)

Dim dr As DataRow = CType(CType(e.Item.DataItem, DataRowView).Row, DataRow) '允许您访问绑定到行 ex 的所有数据。博士(“ID”).ToString

将 ph 调暗为 PlaceHolder = CType(e.Item.FindControl("phResults"), PlaceHolder)

' 以编程方式创建 AutoCompleteExtender && 设置属性

' 以编程方式创建触发所需 JavaScript 的按钮

' 使用 "ph.Controls.Add( ctrl ) 将控件添加到 PlaceHolder

结束子`

于 2009-03-05T20:36:49.117 回答