在自定义控件中从javascript调用c#方法

在自定义控件中从javascript调用c#方法

本文介绍了在自定义控件中从javascript调用c#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个继承自checkboxlist控件的Multi Select下拉列表自定义控件。它有一个title属性,它将被设置为div标签中呈现的下拉控件的默认文本,其中的图像显示下拉图标,如下面的代码所示。我还嵌入了一个javascript函数,它将事件监听器附加到渲染的checkboxlist复选框。我想要做的是,当复选框选中状态更改时,应该从javascript设置Title属性。或者应该调用c#方法,以便可以设置Title属性。请指导我如何做到这一点。



我的自定义控制代码

I am trying to create a Multi Select dropdown list custom control inheriting from the checkboxlist control. it has a title property which will be set as the default text of the dropdown control rendered in a div tag with an image to show dropdown icon as in the code below. I also have a javascript function embedded which will attach event listener to the rendered checkboxlist checkboxes. What i want to do is, when the checkbox checked state is changed the Title property should be set from the javascript . or a c# method should be called so that Title property can be set. Please guide me on how this can be done.

my custom control code

[ToolboxData("<{0}:CheckBoxDropDown  runat="server"></{0}:CheckBoxDropDown>")]
    public class CheckBoxDropDown : CheckBoxList
    {
public string Title { get; set; }

 protected override void Render(HtmlTextWriter writer)
        {
 string divFirstRow = @" <div id='{3}'> {0} <img id=""{1}"" src=""{2}""/></div>";

            writer.WriteLine(string.Format("<div class=\"{0}\" >", this.CssClass));

 writer.Write(string.Format(divFirstRow, this.Title + "  ",
        base.ClientID + "_arrowDown", ImageURL, base.ClientID + "_Title"));
            writer.WriteLine();

base.Render(writer); //render the checkboxlist under the div

            writer.WriteLine("</div>");





这是我从自定义控件渲染的javascript代码。





this is my javascript code that is rendered from the custom control.

window.onload = function () {
            for ( i = 0; i < checkbox.length; i++) {
                checkbox[i].attachEvent(""onclick"", checkChanged );
            }
        }


            function checkChanged() {
                strTitle='';
             for ( i = 0; i < checkbox.length; i++) {
                if(checkbox[i].checked==true)
                {
                      strTitle+=checkbox[i].value+ ";";
                }
                }
                CHK.Title=strTitle; // This has error
            }





我在strTitle中获得所选的值。但无法将其设置为Title属性。



I get the selected values in strTitle. but not able to set it as the Title property.

推荐答案

AddAttributesToRender


这篇关于在自定义控件中从javascript调用c#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:51