Morignus
Morignus

Reputation: 95

How to access a c# variable's value in an .aspx page and use it inside String.Concat method?

Hi I have this in my code behind:

string input = Request.Url.AbsoluteUri;
string output = input.Substring(input.IndexOf('=') + 1);

This is my .aspx page:

<asp:TemplateField ShowHeader="False" HeaderText="Some text">
   <ItemTemplate>
     <asp:TextBox ID="txtUrl" Text='<%# String.Concat(@"\Uploads\",Eval("text")) %>' runat="server" onclick="javascript:this.select();"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

And I need to do this:

<asp:TemplateField ShowHeader="False" HeaderText="Some text">
   <ItemTemplate>
     <asp:TextBox ID="txtUrl" Text='<%# String.Concat(@"\Uploads\",output,Eval("text")) %>' runat="server" onclick="javascript:this.select();"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

- As you can see, I need to create custom "path" in textbox using string.concat joining \Uploads\ + value of variable output + Eval("text")

Thank you very much in advance!

Upvotes: 2

Views: 919

Answers (2)

emerson.marini
emerson.marini

Reputation: 9348

Declare it public.

public string output = string.Empty; // At the class declaration

output = input.Substring(input.IndexOf('=') + 1); // On page load, etc

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could start by writing a method in your code behind:

protected string GetOutput()
{
    string input = Request.Url.AbsoluteUri;
    return input.Substring(input.IndexOf('=') + 1);
}

and then consume this method in your WebForm:

<asp:TemplateField ShowHeader="False" HeaderText="Some text">
   <ItemTemplate>
     <asp:TextBox ID="txtUrl" Text='<%# String.Concat(@"\Uploads\", this.GetOutput(), Eval("text")) %>' runat="server" onclick="javascript:this.select();"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

Upvotes: 1

Related Questions