Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag 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

Avatar of Michael Sterling
Michael Sterling
Flag of United States of America image

ASKER

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that found my contentplaceholder but still missing the textbox (input)...
try this instaed of new


  TextBox txb = (TextBox)cph.FindControl("txtStudentIdSearchBox");
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still pullin a null on the text box
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use runat=server for input like this


 <input id="txtStudentIdSearchBox" type="text" class="studentIdInput" runat="server"  />
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what control do i declare the input control to be? can't cast the input to a textbox....
use runat=server for input like this


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

<asp:TextBox id="txtStudentIdSearchBox" runat="server"></asp:TextBox>
what if my code depends on it being an input?
i need it to be an input because i'm running some javascript on .keypress property of the input control. the TextBox control...
With you input code try this

Control txb = cph.FindControl("txtStudentIdSearchBox");
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"  />

@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

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.
@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

@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...
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>
@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...
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.