0

I'm trying to set a value for TextLabel when the the LinkButton MessageName is clicked. However I'm not having any luck.

I tried using DataList.SelectedItem.FindControl("blah") as shown here but SelectedItem keeps returning null.

I also tried something like this stackoverflow question but it's not working as well.

<asp:DataList
   ID="dlMessages"
   runat="server"
   DataSourceID="dsMessages">
<EditItemStyle Font-Names="Courier New" />
<ItemStyle BorderStyle="NotSet" />
<ItemTemplate>
  <div>
    <table>
      <tr>
        <td><asp:LinkButton ID="MessageName" Text="Some stuff" runat="server" /></td>
        <td>...</td>
      </tr>
    </table>
    <asp:Label ID="TextLabel" runat="server />
  </div>
</ItemTemplate>

 protected void DataList_OnItemCommand(object sender, EventArgs e)
 {
     if (dlMessages.SelectedItem == null)
         return;
     DagSelect.Text = ((LinkButton) dlMessages.SelectedItem.FindControl("MessageName")).Text;
     Label l = (Label) dlMessages.SelectedItem.FindControl("TextLabel");
     l.Text = DagSelect.Text;
 }

Does anyone know a good way to do this? Basically, I only want the text to be displayed when the item is selected. When it's not, the string should remain empty/invisible.

1 Answer 1

1

Add CommandName="Select" to your LinkButton, then change your event to the SelectedIndexChanged event on your DataList.

<asp:LinkButton CommandName="Select" ID="MessageName" Text="Some stuff" runat="server" />
Sign up to request clarification or add additional context in comments.

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.