0

There is a search box on the Master page that takes a value and calls code that provide a details page on that item. The Master page form takes a 'butterfly name' and the button calls the details page on that butterfly.

When I try to run this code from a form based on the Mater page, I receive:

'System.NullReferenceException: 'Object reference no set to an instance of an object.' Systems.Collections.Specialized.NameValueCollection.this[string].get returned null

Master Page ASPX

<input id="searchButterflies" runat="server" type="text" class="form-control mr-2" placeholder="Butterfly Name" style="width: 170px; font-size: .8em;" />
<asp:Button runat="server" ID="btnButterflySearch" CssClass="btn btn-outline-light" OnClick="btnButterflySearch_Click" Text="Search" />

Master Page Code-behind

protected void btnButterflySearch_Click(object sender, EventArgs e)
{
    string btrfly = Request.Form["searchButterflies"].ToString();
    Session["SearchString"] = btrfly;
    Session["SearchString"] = searchButterflies.ToString();
    Response.RedirectPermanent("../BU/BU_SearchName.aspx?Type=SB" + "&Group=" + btrfly);
}

Thank you for any help you can provide.

Susan Farrar
  • 83
  • 1
  • 6
  • Possible duplicate: https://stackoverflow.com/questions/53774728/how-to-use-runat-server-in-input-tag See the marked answer there for what you're doing wrong here. You're using the searchButterflies like it was a name, but it's the id. – Nikki9696 Jun 15 '21 at 14:31
  • Thank you for your comment, but I'm not sure I understand. The code snippets have worked for years on stand-alone pages. It is only when I moved the snippets to a Master page that the code stopped functioning and caused an error on the line that starts with 'string btrfly ='. – Susan Farrar Jun 16 '21 at 13:31
  • Hmmm. Why do you have this `Request.Form["searchButterflies"]` which seems to refer (from what I see?) to the same control as this `searchButterflies.ToString();` Request.Form will be looking for something with [a name](https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525985(v=vs.90)) of searchButterflies but all I see is a control with that as the ID. Is something posting that somewhere else? – Nikki9696 Jun 16 '21 at 14:46
  • I see your confusion. That line of code was merely for testing and I have removed that line from the code example. I merely placed that line of code to test whether or not the ID of 'searchButterflies' was visible in the code-behind. – Susan Farrar Jun 17 '21 at 13:16

0 Answers0