Avatar of Michael Sterling
Michael Sterling
Flag for United States of America asked on

How can I find my input control?

I've got an input control that i'm trying to find in a ContentPlaceHolder and i'm having no luck. I'm including the markup and the code behind. Any help would be appreciated.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:HiddenField runat="server" ID="hfAccessLevel" Value='<%# Eval("Page.AccessLevel") %>' />
    <div class="studentIdSearch">
        <div class="srchContainer">
            <fieldset>
                <h2>
                    <asp:Label runat="server" ID="lblPrompt" Text="To sign in or out, enter (or swipe) student id here:"
                        CssClass="prompt"></asp:Label>
                    <input id="txtStudentIdSearchBox" type="text" class="studentIdInput" />
                    <asp:Panel ID="pnlButton" runat="server">
                        <asp:Button ID="lnkbtnFindStudent" runat="server" Text="Find Student" CssClass="btnSearch" OnClick="lnkbtnFindStudent_Click" >
                        </asp:Button></asp:Panel>
                </h2>

.
.
.

CODE BEHIND


    protected void lnkbtnFindStudent_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder cph = new ContentPlaceHolder();
        TextBox txb = new TextBox();
        StudentSignInSignOutRec ssisor = new StudentSignInSignOutRec();        

        string studentID;

        cph = (ContentPlaceHolder)FindControl("MainContent");

        if (cph != null)
            txb = (TextBox)cph.FindControl("txtStudentIdSearchBox");

        if (txb != null)
        {
            studentID = txb.Text.Trim();
            ssisor = Utility.DoesStudentExist(studentID);
            if (ssisor != null)
                Utility.SignInSignOutStudent(ssisor);
            //else show message student does not exist
        }
    }

Open in new window

ASP.NETC#ASP

Avatar of undefined
Last Comment
Kaushal Arora

8/22/2022 - Mon
Michael Sterling

ASKER
...i should add that the current code behind (above) is returning a cph null and sebsequently so is the textbox...
ASKER CERTIFIED SOLUTION
Pratima

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michael Sterling

ASKER
that found my contentplaceholder but still missing the textbox (input)...
Pratima

try this instaed of new


  TextBox txb = (TextBox)cph.FindControl("txtStudentIdSearchBox");
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Michael Sterling

ASKER
still pullin a null on the text box
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Pratima

use runat=server for input like this


 <input id="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server"  />
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Michael Sterling

ASKER
what control do i declare the input control to be? can't cast the input to a textbox....
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Pratima

use runat=server for input like this


 <input id="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server"  />
Pratima

use this

<asp:TextBox id="txtStudentIdSearchBox" runat="server"></asp:TextBox>
Michael Sterling

ASKER
what if my code depends on it being an input?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Michael Sterling

ASKER
i need it to be an input because i'm running some javascript on .keypress property of the input control. the TextBox control...
Pratima

With you input code try this

Control txb = cph.FindControl("txtStudentIdSearchBox");
Pratima

Got it

try this

HtmlInputText txb = (HtmlInputText)cph.FindControl("txtStudentIdSearchBox");


get the value like

studentID = txb.Value.Trim();


And add runat=server

<input id="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server"  />

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Michael Sterling

ASKER
@pratima_mcs: ironically enough,...i did just that the problem with it, is that my javascript for the kepress no longer works. my goal is to, if necessary, trim a certain number of characters from the left and right side of the string in the input. for some reason when i put in the runat="server" property, the javascript no longer fires...


$("#txtStudentIdSearchBox").keypress(function (event) {
                        myLength = $(this).val().length;
                        if (myLength == 17) {
                            value = $("#txtStudentIdSearchBox").val();
                            value = value.substring(8);
                            value = value.substring(0, value.length - 2);
                            $("#txtStudentIdSearchBox").val(value);
                        }

Open in new window

codingbeaver

Change your Input control to a TextBox control as suggested before.

>>>
i need it to be an input because i'm running some javascript on .keypress property of the input control. the TextBox control...

You still can use .keypress event on TextBox control. After the page is rendered to brower, TextBox control will become an Input field.

Using TextBox will make your life a lot easier.
Michael Sterling

ASKER
@codingbeaver: i've tried using an <asp:TextBox> control and while everything else works the same and is arguably simpler,..the keypress does not fire in my javascscript / jQuery code,...for me.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:HiddenField runat="server" ID="hfAccessLevel" Value='<%# Eval("Page.AccessLevel") %>' />
    <div class="studentIdSearch">
        <div class="srchContainer">
            <fieldset>
                <h2>
                    <asp:Label runat="server" ID="lblPrompt" Text="To sign in or out, enter (or swipe) student id here:"
                        CssClass="prompt"></asp:Label>
                    <%--<input id="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server"/>--%>
                    <asp:TextBox id="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server"/>
                    <asp:Panel ID="pnlButton" runat="server">
                        <asp:Button ID="lnkbtnFindStudent" runat="server" Text="Find Student" CssClass="btnSearch" OnClick="lnkbtnFindStudent_Click" >
                        </asp:Button></asp:Panel>
                </h2>
                <script type="text/javascript" src="http://api.jquery.com/scripts/events.js"></script>
                <script type="text/javascript">
                    var xTriggered = 0;
                    var myLength = 0;
                    var myString;
                    var value;
                    $("#txtStudentIdSearchBox").keypress(function (event) {
                        myLength = $(this).val().length;
                        if (myLength == 17) {
                            value = $("#txtStudentIdSearchBox").val();
                            value = value.substring(8);
                            value = value.substring(0, value.length - 2);
                            $("#txtStudentIdSearchBox").val(value);
                        }
                        if (event.which == 13) {
                            event.preventDefault();
                        }

                        //xTriggered++;
                        //var msg = "Handler for .keypress() called " + xTriggered + " time(s).";

                        //$.print(msg, "html");
                        //$.print(event);
                    });

                    $("#other").click(function () {
                        $("#target").keypress();
                    });
                </script>
            </fieldset>
            <div class="print-output-line">
            </div>
        </div>
    </div>
    <div>
        <sttc:StudentsSignedInGrid ID="MainGrid" runat="server" ResultType="1" ucAccessLevel='<%# Eval("Page.AccessLevel") %>' />
    </div>
</asp:Content>

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Michael Sterling

ASKER
@codingbeaver: and further,...not only does it seem to only work for the input control, for me,...but it will only work if the runat="sever" attribute is not there...
nishant joshi

change your script with keyup......

<script type="text/javascript">
                    var xTriggered = 0;
                    var myLength = 0;
                    var myString;
                    var value;
                    $("#txtStudentIdSearchBox").keyup(function (event) {
                        myLength = $(this).val().length;
                        if (myLength == 17) {
                            value = $("#txtStudentIdSearchBox").val();
                            value = value.substring(8);
                            value = value.substring(0, value.length - 2);
                            $("#txtStudentIdSearchBox").val(value);
                        }
                        if (event.which == 13) {
                            event.preventDefault();
                        }

                        //xTriggered++;
                        //var msg = "Handler for .keypress() called " + xTriggered + " time(s).";

                        //$.print(msg, "html");
                        //$.print(event);
                    });

                    $("#other").click(function () {
                        $("#target").keypress();
                    });
                </script>
Michael Sterling

ASKER
@nishantcomp2512: i tried what you suggested, thank you. how ever the result was the same. the .keypress and .keyup events only seem to get "picked up" or "fire" when i'm using an <input> control. when i switch to an <asp:TextBox> control i get nada, nunca, zilch, zip, zero...unless i stumble upon something, i'm probably going to have to go with the textbox control using the runat server tag so that i can find it in the code behind. the "magic" of the .keypress and or .keyup events that i'm trying to use will have to be left out. they both work, on an input control without the runat="sever" property, but then i have no way to access the control (no way to get a hold of it) in the code behind. this is my dilemma. ill leave this open for a few days while i "come to terms with the reality of this" ha...as well as just in case someone does come up with a solution...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Kaushal Arora

Hi, the only solution you can get in this situation is as follows:

1. Add a new hidden input field as
      <input id="txtStudentIdSearchBoxhidden" type="text" class="studentIdInput" style="display: none; visibility: hidden;" runat="server"  />

Mark this as runat="server", coz we will get this input field in server side code and have the previous input field as it, mean without runat attribute.

2. Now add a onclientclick event on your linkbutton code which will add the "txtStudentIdSearchBox" input field value in "txtStudentIdSearchBoxhidden" input and then you can use this in the server code by finding it the same way you were doing for the previous input field.
E.g:
<asp:Button ID="lnkbtnFindStudent" runat="server" Text="Find Student" CssClass="btnSearch" OnClick="lnkbtnFindStudent_Click" OnClientClick="AnyJavascriptFunction();" >
                        </asp:Button>

<script type="text/javascript">
function AnyJavascriptFunction()
{
  document.getElementById('<%= txtStudentIdSearchBoxhidden.ClientID %>').value = document.getElementById('txtStudentIdSearchBox').value;
}
</script>

And then you can find the HTMLInputText control in the server side for "txtStudentIdSearchBoxhidden" and thats it you are having the value for this input text control.

Hope it helps.