开发者

Enable/disable text box placed in gridview on check event of check box with javascript

I have gridview which contains check box and textbox in different item template fields. Text boxes will be disbled when grid is loaded. Now what I want is, when user checks any of the check box, respected textbox in that particular row should be enabled. When user unchecks the checked checkbox, again the textbox should get clear and disable. Now when user clicks on the save button, a javascript should validate all the textbox whose respected checkboxes are checked.

Validation is

(1)Text boxes should not be empty

(2)It should allow only character and space(I want to enter name in that text box). How can I write java script to perform all this task. I am novice to javascript and开发者_运维百科 completely unaware of its concept.

Thanks in advance


<script type="text/javascript">
    var nameRegex = /^[a-zA-Z ]+$/;
    function validateName(sender, args) {
        args.IsValid = nameRegex.test(args.Value);
    }
</script>


<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" DataKeyNames="Id" >
    <Columns>
        <asp:TemplateField >
            <ItemTemplate>
                <asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="SelectRow"  />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:TemplateField HeaderText="Name" >
            <ItemTemplate>
                <asp:TextBox runat="server" ID="NameTextBox" Enabled="false" Text='<%# Eval("Name") %>' />
                <asp:CustomValidator runat="server" ID="NameValidator" ControlToValidate="NameTextBox"
                    ValidateEmptyText="true" Text="!" Display="Static" Enabled="false"
                    ClientValidationFunction="validateName" OnServerValidate="ValidateName" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


public partial class WebForm1 : System.Web.UI.Page
{
    private static readonly Regex nameRegex = new Regex("^[a-zA-Z ]+$");

    protected void ValidateName(object source, ServerValidateEventArgs args)
    {
        args.IsValid = nameRegex.IsMatch(args.Value);
    }

    protected void SelectRow(object sender, EventArgs e)
    {
        Page.Validate();
        var checkBox = (CheckBox)sender;

        if (Page.IsValid || !checkBox.Checked)
        {
            var textBox = (TextBox)checkBox.NamingContainer.FindControl("NameTextBox");
            var nameValidator = (CustomValidator)checkBox.NamingContainer.FindControl("NameValidator");

            textBox.Enabled = checkBox.Checked;
            nameValidator.Enabled = checkBox.Checked;
            if (!checkBox.Checked)
                textBox.Text = "";
        }
        else
        {
            checkBox.Checked = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = from item in Enumerable.Range(0, 10)
                                   select new
                                   {
                                       Id = item,
                                       Name = "" 
                                   };
            GridView1.DataBind();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜