ItemDataBound

Last post 03-25-2008 5:31 AM by cyart. 7 replies.

Sort Posts:

  • ItemDataBound

    03-18-2008, 4:25 PM
    • Loading...
    • cyart
    • Joined on 03-18-2008, 6:07 AM
    • Posts 10

    Hello,

    I am trying to assign some javascript to a html row. However I believe should be casting this as a datarow view is this correct as this as I am receieving object reference not set to an instance. What would the vb.net syntax be to cast the dataview row to a htmlrowview if this is the case.

     

    Thanks 

     

    Protected Sub rptItems_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptItems.ItemDataBound


            Dim row As HtmlTableRow

            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                row = CType(e.Item.FindControl("sms_received_log"), HtmlTableRow)
                row.Attributes.Add("onclick", "javascript:Repeater_selectRow(this, " + row.ClientID + ",true);")
               End If
        End Sub

     

    <asp:Repeater ID="rptItems" runat="server" EnableViewState="false">
                       <ItemTemplate>
                           <table>
                               <tr id='<%#Eval("sms_received_log_id")%>'>
                                   <td style="width:40px;text-align:center " >
                                   <asp:TextBox ID="txtID" runat="server" Text='<%#Eval("sms_received_log_id") %>' Visible=false></asp:TextBox>
                                       <asp:CheckBox ID="chkRemoved" runat="server" Checked='<%# Convert.ToBoolean(Eval("removed")) %>'   AutoPostBack="false"/></td>
                                   
                                   <td style="width:40px;text-align:center "  >
                                       <asp:Image ID="Image1" runat="server" Width="10px" Height="10px" /></td>
                                   <td style="width:170px;text-align:left " >
                                       <asp:TextBox ID="txtClientName" runat="server"  BorderStyle="none"
                                           BorderWidth="0" Font-Names="Verdana" Font-Size="8pt" ReadOnly="true"
                                           Text='<%#Eval("client_name") %>' Width="170"   >
                                                    </asp:TextBox>
                                                    <asp:TextBox ID="txtBody" runat="server" Text='<%#Eval("body") %>' Visible=false></asp:TextBox>
                                                    </td>
                                       
                                   
                                   <td style="width:100px;text-align:left" >
                                        <asp:TextBox ID="txtSMSReceive" runat="server" BackColor="#F7F7F7" BorderStyle="none"
                                           BorderWidth="0" Font-Names="Verdana" Font-Size="8pt" ReadOnly="true" Rows="3"
                                           Text='<%#Eval("sms_receive_date") %>' Width="100" Wrap="true">
                                                    </asp:TextBox>
                                   </td>
                               </tr>
                           </table>
                       </ItemTemplate>
                       <FooterTemplate>
                            
                       </FooterTemplate>
                   </asp:Repeater>

     

  • Re: ItemDataBound

    03-19-2008, 2:48 AM
    Answer
    • Loading...
    • ASP.NET Dev
    • Joined on 12-09-2006, 7:20 AM
    • Kozhikode, Kerala, India
    • Posts 259

    Hi,

    As per my understanding your requirment is to pass the sms_recieved_log_id to javascript function on table row click, right?

    If so, modify your ItemDataBound method as follows

     

    Dim row As HtmlTableRow
    Dim txtLogID As TextBox
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    	row = CType(e.Item.FindControl("sms_received_log"), HtmlTableRow)
    	txtLogID = CType(e.Item.FindControl("txtID"), TextBox)
            row.Attributes.Add("onclick", "javascript:Repeater_selectRow(this, " + txtLogID.Text + ",true);")
    End If
    

     

    Then your table row

    <tr id='<%#Eval("sms_received_log_id")%>'>

    to

    this

    <tr id="sms_received_log_id" runat="server">

    Explanation:

    1. You have to add runat="server" attribute to  FindControl work

    2. If you add runat="server" you doesn't able to set id dynamically  i.e. id='<%#Eval("sms_received_log_id")%>' will not work

     

    Try this and let me know the results

    Regards,
    Sandeep Kumar
    MCTS - .NET Framework 2.0 (Web Applications)
    --------------------------------------------------
  • Re: ItemDataBound

    03-19-2008, 5:09 AM
    • Loading...
    • cyart
    • Joined on 03-18-2008, 6:07 AM
    • Posts 10

    Hello Thanks for the response. I will eventually pass the txtLogID into the javascript function. However I still need to be able to change the backcolor of the selected row. So I was trying to pass the selected row into the Javascriot function in the first instance to ensure I had this working correctly.

     

    Will your solution be enable me to pass the row to the javascript function?

    Regards

    Paul 

     


     

  • Re: ItemDataBound

    03-19-2008, 6:24 AM
    Answer
    • Loading...
    • ASP.NET Dev
    • Joined on 12-09-2006, 7:20 AM
    • Kozhikode, Kerala, India
    • Posts 259

    Hi,

    You are already passing current row(as this) to the js function

    row.Attributes.Add("onclick", "javascript:Repeater_selectRow(this, " + txtLogID.Text + ",true);")

    You can set the row background color by accessing the this

    eg.

    function Repeater_selectRow(row, id, flag)

    {

    row.style.backgroundColor='red';

    }

    Hope this helps

    Regards,
    Sandeep Kumar
    MCTS - .NET Framework 2.0 (Web Applications)
    --------------------------------------------------
  • Re: ItemDataBound

    03-21-2008, 4:47 AM
    • Loading...
    • cyart
    • Joined on 03-18-2008, 6:07 AM
    • Posts 10

    Hello, the row colour is changing fine, however I have modified the itemdatabound to the following, and the javascript function is now as follows. However, I am receiving a javascript error Line 227 char 17 "Object Required" which when looking at the source iit is on the line var txt declaration var txt = document.getElementById(ProductId);

     

     function Repeater_selectRow(row, ProductId)
                {            
                    var txt = document.getElementById(ProductId);

                    if (lastRowSelected != row)
                    {
                    alert(txt.value);
                        
                        if (lastRowSelected != null)
                        {
                        //alert('in');
                            ProductId.backgroundColour = 'white'
                            lastRowSelected.style.backgroundColor = 'white';
                            lastRowSelected.style.color = 'black'
                            //lastRowSelected.style.fontWeight = 'normal';
                        }
                        //originalColor = row.style.backgroundColor
                        row.style.backgroundColor = '#E97300'
                        ProductId.background = '#E97300'
                        row.style.color = 'White'
                        //row.style.fontWeight = 'normal'
                        lastRowSelected = row;
                        
                    }
                    
                }

     

      Protected Sub rptItems_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptItems.ItemDataBound


            Dim row As HtmlTableRow
            Dim txtClientName As TextBox
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                row = CType(e.Item.FindControl("sms_received_log_id"), HtmlTableRow)
                txtClientName = CType(e.Item.FindControl("txtClientName"), TextBox)
                row.Attributes.Add("onclick", "javascript:Repeater_selectRow(this, " + txtClientName.ClientID + ",true);")
               End If
        End Sub

     

    <asp:Repeater ID="rptItems" runat="server" EnableViewState="false">
                       <ItemTemplate>
                           <table>
                               <tr id="sms_received_log_id" runat="server">
                                   <td style="width:40px;text-align:center " >
                                   <asp:TextBox ID="txtID" runat="server" Text='<%#Eval("sms_received_log_id") %>' Visible=false></asp:TextBox>
                                       <asp:CheckBox ID="chkRemoved" runat="server" Checked='<%# Convert.ToBoolean(Eval("removed")) %>'   AutoPostBack="false"/></td>
                                  
                                   <td style="width:40px;text-align:center "  >
                                       <asp:Image ID="Image1" runat="server" Width="10px" Height="10px" /></td>
                                   <td style="width:170px;text-align:left " >
                                       <asp:TextBox ID="txtClientName" runat="server"  BorderStyle="none"
                                           BorderWidth="0" Font-Names="Verdana" Font-Size="8pt" ReadOnly="true"
                                           Text='<%#Eval("client_name") %>' Width="170"   >
                                                    </asp:TextBox>
                                                    <asp:TextBox ID="txtBody" runat="server" Text='<%#Eval("body") %>' Visible=false></asp:TextBox>
                                                    </td>
                                      
                                  
                                   <td style="width:100px;text-align:left" >
                                        <asp:TextBox ID="txtSMSReceive" runat="server" BackColor="#F7F7F7" BorderStyle="none"
                                           BorderWidth="0" Font-Names="Verdana" Font-Size="8pt" ReadOnly="true" Rows="3"
                                           Text='<%#Eval("sms_receive_date") %>' Width="100" Wrap="true">
                                                    </asp:TextBox>
                                   </td>
                               </tr>
                           </table>
                       </ItemTemplate>
                       <FooterTemplate>
                           
                       </FooterTemplate>
                   </asp:Repeater>

  • Re: ItemDataBound

    03-21-2008, 4:55 AM
    • Loading...
    • cyart
    • Joined on 03-18-2008, 6:07 AM
    • Posts 10

     Sorry this is the source that has been generated

     

    <script language="javascript">
        var lastColorUsed;
        var lastRowSelected;
        var lastTxtbox1;
        var originalColor;
                function Repeater_selectRow(row, ProductId)
                {           
                    var txt = document.getElementById(ProductId);
                    alert(txt.Text);
                    if (lastRowSelected != row)
                    {
                   
                       
                        if (lastRowSelected != null)
                        {
                        //alert('in');
                            ProductId.backgroundColour = 'white'
                            lastRowSelected.style.backgroundColor = 'white';
                            lastRowSelected.style.color = 'black'
                            //lastRowSelected.style.fontWeight = 'normal';
                        }
                        //originalColor = row.style.backgroundColor
                        row.style.backgroundColor = '#E97300'
                        ProductId.background = '#E97300'
                        row.style.color = 'White'
                        //row.style.fontWeight = 'normal'
                        lastRowSelected = row;
                       
                    }
                   
                }
               
                function setTextboxVals(chkID, txtID)
                {
                      var chk = document.getElementById(chkID);
                      var txt = document.getElementById(txtID);

                      if (chk.checked == true)
                      {
                            txt.disabled = false;
                            //txt.style.backgroundColor = '#E97300';
                            //txt.value = "User did not want to answer this question";
                      }
                      else
                      {
                            txt.disabled = false;
                            txt.value = "";
                      }           
                     
                      return true;
                }
               
               
               
       
        </script>

    <table align="left" border="0" cellpadding="0" cellspacing="0" width="541">
          <tr>
           <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="542">
              <tr>
               <td style="background-image: url(../Template-images/Main_Inbox_Header1.gif); width: 34px; height: 25px; background-repeat: no-repeat;"></td>
               <td style="background-image: url(../Template-images/Main_Inbox_Header2.gif); width: 508px; height: 25px; background-repeat: no-repeat;"></td>
              </tr>
            </table></td>
          </tr>
          <tr>
           <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="542">
              <tr>
               <td><table align="left" border="0" cellpadding="0" cellspacing="0" width="354">
                  <tr>
                   <td style="background-image: url(../Template-images/Main_Inbox_Body.gif); width: 354px; height: 130px; background-repeat: no-repeat; vertical-align: top; text-align: left;">
                  
                           <table>
                               <tr id="ctl00_ContentPlaceHolder1_rptItems_ctl00_sms_received_log_id" onclick="javascript:Repeater_selectRow(this, ctl00_ContentPlaceHolder1_rptItems_ctl00_txtClientName,true);">
        <td style="width:40px;text-align:center ">
                                  
                                       <input id="ctl00_ContentPlaceHolder1_rptItems_ctl00_chkRemoved" type="checkbox" name="ctl00$ContentPlaceHolder1$rptItems$ctl00$chkRemoved" /></td>
        <td style="width:40px;text-align:center ">
                                       <img id="ctl00_ContentPlaceHolder1_rptItems_ctl00_Image1" src="" style="height:10px;width:10px;border-width:0px;" /></td>
        <td style="width:170px;text-align:left ">
                                       <input name="ctl00$ContentPlaceHolder1$rptItems$ctl00$txtClientName" type="text" value="Craig Spencer" readonly="readonly" id="ctl00_ContentPlaceHolder1_rptItems_ctl00_txtClientName" style="border-width:0px;border-style:None;font-family:Verdana;font-size:7pt;height:10px;width:170px;" />
                                                   
                                                    </td>
        <td style="width:100px;text-align:left">
                                        <input name="ctl00$ContentPlaceHolder1$rptItems$ctl00$txtSMSReceive" type="text" value="17-3-2008" readonly="readonly" id="ctl00_ContentPlaceHolder1_rptItems_ctl00_txtSMSReceive" style="background-color:#F7F7F7;border-width:0px;border-style:None;font-family:Verdana;font-size:7pt;height:10px;width:100px;" />
                                   </td>
    </tr>

  • Re: ItemDataBound

    03-24-2008, 5:57 AM
    Answer

    Hi cyart,

    The number of parameters in your javascript is not equal. You lost your flag object in your Repeater_selectRow(row, ProductId) definition.

    Thanks,

    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: ItemDataBound

    03-25-2008, 5:31 AM
    • Loading...
    • cyart
    • Joined on 03-18-2008, 6:07 AM
    • Posts 10

     I have now resolved this problem thanks for all your help

Page 1 of 1 (8 items)
Microsoft Communities
Page view counter