Robin Rieger
Robin Rieger

Reputation: 1194

Find Control in code behind - multiple results controls - nth occurence

I have the following problem.

I have a asp:textbox on the page, runat server with an id of say txt This text box is in a <div>, nothing special. ie:

<div>
 <asp:TextBox id="txt" runat="server"></asp:TextBox>
</div>

The problem is there is some java script which when you push the corresponding button it doubles (copies) the div. This is by design. It is meant to.

When you hit save but at the bottom of the page on a asp:Button, it can't find the value I need because it returns two results.

In the code behind:

(Textbox) blah = (Textbox)senderbutton.FindControl("txt");

string test = blah.text

But the result is essentially--> "The value in the textbox , The value in the textbox"

I.e. it is there twice. I have worked around this by doing the following:

string[] test = blah.text.split(new[] { ',' })

and then only calling the second value in the array or whatever.

BUT, now I have this situation but the problem is that a user can enter a string with a ' , ' in it, hence the splitting goes to crap....

So can I find a control with an id, but only find the nth occurence of it in the code behind?

Upvotes: 0

Views: 1184

Answers (4)

Jith
Jith

Reputation: 1701

Seems you need to give different name(like txt0,txt1...) for each copy of the input controls. You can do this using javascript up on client click(prior to form submission) of your asp button

-- Javascript method

    function ModifyName() {
        var x = 0;
        $("input[name='txt']").each(function () {
            $(this).attr("name", $(this).attr("name") + x);
            x++;
        });
    }

-- asp:Button

<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
   OnClientClick="ModifyName();" onclick="btnSubmit_Click" />

So in code behind you can get the values like this...

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        var resultArr = Request.Form.AllKeys.Where(x => x.Contains("txt"))
            .Select(x => Request.Form[x]).ToArray();
    }

Upvotes: 1

Cheng Chen
Cheng Chen

Reputation: 43523

The best solution would be: avoiding copying the div in js. Since you said "This is by design. It is meant to."(even I really doubt it), there are some alternative solutions:

(1) Don't use the default submit behavior of the form. That is, in the click (js) event of the save button, organize the data in the form and then submit it.

(2) Modify the second(copied) textbox's id so that its id is different from the original one, and then get the data in code behind.

Upvotes: 0

Kaf
Kaf

Reputation: 33809

I am not sure why you using FindControl method to find the control when you can directly access the txt control from code behind.

You can get results easily

String test = txt.Text;

Upvotes: -1

geedubb
geedubb

Reputation: 4057

Not a very nice solution, but you could examine the Request.Form collection directly upon postback and write some code to process your dynamically added textbox fields.

Upvotes: 0

Related Questions