0

I have a Response.Redirect on a buttonclick with two querystring-parameters that hold the value from two textboxes. If I leave the textboxes empty I get this URL: /Order.aspx?LastName=&FirstName=. I want to replace the NULL value with "%20" like this /Order.aspx?LastName=%20&FirstName=%20.

Please help my with the if-statements to changes this, I'm really new to this. Here is my code:

protected void btnSearchFirstLastName_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Order.aspx?LastName=" + SearchLastName.Text.Trim() + "&FirstName=" + SearchFirstName.Text.Trim());
}

3 Answers 3

2

Check string using string.IsNullOrEmpty, if true then set something of your choice like " " or string.Empty.

protected void btnSearchFirstLastName_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Order.aspx?LastName=" 
    + string.IsNullOrEmpty(SearchLastName.Text) ? " ": SearchLastName.Text.Trim() 
    + "&FirstName=" + string.IsNullOrEmpty(SearchFirstName.Text) ? " " : SearchFirstName.Text.Trim());
}
Sign up to request clarification or add additional context in comments.

Comments

2
protected void btnSearchFirstLastName_Click(object sender, EventArgs e)
{
    string firstName = "%20";
    string lastName = "%20";

    if (!string.IsNullOrWhitespace(SearchFirstName.Text)
        firstName = SearchFirstName.Text.Trim();

    if (!string.IsNullOrWhitespace(SearchLastName.Text)
        lastName = SearchLastName.Text.Trim();

    Response.Redirect("~/Order.aspx?LastName=" + lastName + "&FirstName=" + firstName);
}

1 Comment

No probs, glad to help :)
0

I think you can simply add this condition that if textbox is empty, then text should be as mentioned by you. Like:

If(abc.text!=string.empty || abc.text!=null)
{
param value=abc.text;
}
else
param value="%20";

Note: This is a side option, you can opt if coding paradigms is not a concern here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.