Lucas_Santos
Lucas_Santos

Reputation: 4740

How Can I get the value of a HtmlControl's variable?

I have the follow line in my code

HtmlControl divstatus = (HtmlControl)GridView1.Rows[j].FindControl("divstatus");

and in my .aspx

<div style="width:70px;" id="divStatus" runat="server"><%# Eval("DscStatus")%></div>

How can I get the value of my div in the codebehind using the class HtmlControl ?

My Grid View is too big, so i'll post just a part

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" 
        EnableModelValidation="True" ForeColor="#333333">
        <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="Chamado">
                <ItemTemplate>
                    <div style="width:30px;">
                       <a href='responder.aspx?cod=<%# Eval("Codigo") %>&sta=<%# Eval("StatusTicket_Id") %>&mot=<%# Eval("MotivoTicket_Id") %>'>
                                <%# Eval("Codigo") %>
                            </a>                                                                       
                        <%# montaAnexo(Eval("Anexo").ToString())%>                
                    </div>
                </ItemTemplate>                
            </asp:TemplateField>

Upvotes: 1

Views: 469

Answers (4)

pseudocoder
pseudocoder

Reputation: 4392

Edit: I haven't reviewed your recent edits adding the GridView TemplateField style elements. This may impact my example, so I will update if appropriate.


I would suggest calling a function in your code-behind which returns an HTML string including the entire <div>. Otherwise you're kind of beating around the bush.

Assuming DscStatus is a string value:

Markup:

<%# RenderStatusDiv(Eval("DscStatus") %>

Code:

 protected string RenderStatusDiv(object DscStatus)
    {
        string strReturn = String.Empty;
        if (DscStatus != null)
        {
            string strDscStatus = (string)DscStatus;
            if (intDscStatus = "OK")
            {
                strReturn = "<div style=\"width:70px;\" id=\"divStatus\" runat=\"server\">" + strDscStatus + "</div>";
            }
            else
            { 
                strReturn = "<div style=\"width:70px;color:red;\" id=\"divStatus\" runat=\"server\">" + strDscStatus + "</div>";
            }
        }
        return strReturn;
    }

Upvotes: 1

Mubarek
Mubarek

Reputation: 2689

Update your line above this way

 HtmlControls.HtmlContainerControl divstatus = (HtmlControls.HtmlContainerControl)GridView1.Rows[j].FindControl("divstatus");

Then access its innerText

 divstatus.InnerText

Upvotes: 1

justin.lovell
justin.lovell

Reputation: 675

// repeat template section start. Example <ItemTemplate>
<%
     string color = "blue";
     if (Eval("DscStatus").Equals(1)) { color = "red"; }
%>
<tr style="color: <%# color %>">
   <td>
       ....
       <div style="width:70px;" id="divStatus" runat="server"><%# Eval("DscStatus")%></div>
       ....
   </td>
</tr>
// end template section end: Example </ItemTemplate>

Upvotes: -1

Daniel A. White
Daniel A. White

Reputation: 190907

You will have to POST some data back via ajax or some hidden form field.

Upvotes: 0

Related Questions