getting a element name out of a variable...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MDCdeveloper
    New Member
    • Oct 2008
    • 3

    getting a element name out of a variable...

    Hello all, and thank you in advance for any feedback/help you might post.

    I need help getting a web page element id out of a string variable to be used within my c# code behind page.

    I am trying to display a string in a literal control telling the user what was wrong with the input that they provided in the associated text box. my error string looks like this:
    [code=cpp]
    errorMsg = "litLastNam e-Please enter a valid last name"
    [/code]
    I already have the string split and placed into two variables. this happens in a for loop.

    here is my code thus far in C#:
    [code=cpp]
    {
    string errorMsg = hdnErrorMsg.Val ue;
    litResults.Text = errorMsg;

    //breaks up the mass error string into individual error strings
    string[] errorMsgSplit = errorMsg.Split( '/');
    string strLitErrorElem ent = "";
    string strLitErrorMsg = "";

    for (int i = 1; i < errorMsgSplit.L ength; i++)
    {
    strLitErrorMsg = strLitErrorMsg + errorMsgSplit[i];
    litResults.Text = strLitErrorMsg;

    //splits the individual error strings into the asp lit and lit text
    string[] indErrorMsg = errorMsgSplit[i].Split('-');
    strLitErrorElem ent = indErrorMsg[0];
    strLitErrorMsg = indErrorMsg[1];

    (here is the part where I'm stuck)
    }
    }
    [/code]
    basically i need to get the stuff out of strLitErrorElem ent out so I can use it like this:
    [code=cpp]
    litLastName.tex t = "Please enter a valid Last Name"
    [/code]
    Again, thank you ahead of time for your positive input, help, and/or examples :)
    Last edited by Frinavale; Oct 7 '08, 05:53 PM. Reason: added [code] tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You will have to make some sort of FindControlByNa me(string name) type function. Loop through all the controls, comparing their Name property to the one you want.
    Although, you use a literal control, which isn't really a control at all. (Doesn't end up as a DIV or a SPAN or any other html, just plain text) Are you just using them to provide a "layout" so your error messages show up next to the control that is incorrect?

    Comment

    • MDCdeveloper
      New Member
      • Oct 2008
      • 3

      #3
      Originally posted by Plater
      You will have to make some sort of FindControlByNa me(string name) type function. Loop through all the controls, comparing their Name property to the one you want.
      Although, you use a literal control, which isn't really a control at all. (Doesn't end up as a DIV or a SPAN or any other html, just plain text) Are you just using them to provide a "layout" so your error messages show up next to the control that is incorrect?

      pretty much. I want to use a loop instead of coding all of the literals individually because I'd like to use this on a form that has 55+ different inputs. thanx for the quick response.

      Comment

      • MDCdeveloper
        New Member
        • Oct 2008
        • 3

        #4
        Got it working. here is the updated code:
        [code=cpp]
        string errorMsg = hdnErrorMsg.Val ue;

        //breaks up the mass error string into individual error strings
        string[] errorMsgSplit = errorMsg.Split( '/');

        for (int i = 1; i < errorMsgSplit.L ength; i++)
        {
        //splits the individual error strings into the asp lit and lit text
        string[] indErrorMsg = errorMsgSplit[i].Split('-');

        Literal litError = (Literal)FindCo ntrol(indErrorM sg[0]);
        litError.Text = indErrorMsg[1];
        }
        [/code]
        Thanx!
        Last edited by Frinavale; Oct 7 '08, 05:54 PM. Reason: Added [code] tags

        Comment

        Working...