Using Switch Case in JavaScript

makumbi
 
on Jun 29, 2022 04:04 AM
211 Views

Please how can use the switch JavaScript.

This is what i would like if a person click on cell Name is populated the calno TextBox with the name of the column name and after that would want to use the switch JavaScript command in a way that case Name is selected then the status TextBox should show currently editing Name Or case when country column is selected then the status TextBox should indicate currently editing country.

var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
    prm.add_endRequest(function (sender, e) {
        if (sender._postBackSettings.panelsToUpdate != null) {
            var rowIndex = parseInt(document.getElementById("hfRowIndex").value);
            var row = document.getElementById("StudentGridMarks").rows[rowIndex + 2];
            if (typeof (row) != "undefined") {
                var celIndex = parseInt(document.getElementById("hfCellIndex").value);
                // var controls = row.cells[celIndex].getElementsByTagName("INPUT");
                var controls = row.getElementsByTagName("INPUT");
                for (var i = 0; i < controls.length; i++) {
                        // if (controls[i].id.indexOf("Name") || controls[i].id.indexOf("Remarks") != -1) {
                        if (controls[i].id.indexOf("Name") != -1) {
                        // Focus to the next record.
                        controls[i].focus();
                        break;
                    }
                }
            }
        }
    });
};
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 29, 2022 05:11 AM
on Jun 29, 2022 08:07 AM

Hi makumbi,

Inorder to use Switch statement you need to set the class name for the TextBoxes.

Then inside the Switch statement you need to check with control classname.

Refer below code.

HTML

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <asp:TextBox ID="txtId" runat="server" Text='<%#Eval("Id") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("Name") %>' CssClass="name" ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:TextBox ID="txtCountry" runat="server" Text='<%#Eval("Country") %>' CssClass="country" ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

JavaScript

var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
    prm.add_endRequest(function (sender, e) {
        if (sender._postBackSettings.panelsToUpdate != null) {
            var rowIndex = parseInt(document.getElementById("hfRowIndex").value);
            var row = document.getElementById("gvCustomers").rows[rowIndex + 2];
            if (typeof (row) != "undefined") {
                var controls = row.getElementsByTagName("INPUT");
                for (var i = 0; i < controls.length; i++) {
                    switch (controls[i].className) {
                        case "name":
                            break;
                        case "country":
                            break;
                        default:
                    }
                }
            }
        }
    });
};