动态绑定事件中的object.Attributes.Add()

GameAttend.aspx
<asp:datagrid id="dgGameAttend" 



<asp:TemplateColumn HeaderText
="備 考">
                                        
<HeaderStyle HorizontalAlign="Center" Width="390px"></HeaderStyle>
                                        
<ItemTemplate>
                                            
<asp:Label id="lblMemo" runat="server" Width="390px" Text='<%# DataBinder.Eval(Container, "DataItem.memo") %>'>
                                            
</asp:Label>
                                        
</ItemTemplate>
                                        
<EditItemTemplate>
                                            
<asp:TextBox id=edit_memo runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.memo") %>'>
                                            
</asp:TextBox>
                                        
</EditItemTemplate>
                                    
</asp:TemplateColumn>
                                
</Columns>
                            
</asp:datagrid>

<DIV id="MemoBoard" style="POSITION: relative" ms_positioning="GridLayout"></DIV>

<script language="JavaScript">
        
<!--
            
function memoDisplay(m)
            {
                
var obj = document.all("MemoBoard");
                obj.innerText 
= m;
            }
            
            
function memoDisplay2()
            {
                document.all(
"MemoBoard").innerText = '';
            }
        
//-->
        </script>

GameAttend.aspx.cs
        public string MemoStringSlice(string memo)
        {
            
string text = memo;

            
if (memo.Length >= 10)
            {
                text 
= memo.Substring(010);
                text 
= text + " ";
            }

            
return text;
        }

        
private void dgGameAttend_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label l 
= (Label)e.Item.Cells[4].FindControl("lblMemo");

                
if(l != null)
                {
                    
//l.Attributes.Add("onmouseover", "memoDisplay(" + Convert.ToString(e.Item.ItemIndex + 1) + ");");
                    l.Attributes.Add("onmouseover""memoDisplay('" + l.Text + "');");
                    l.Attributes.Add(
"onmouseout""memoDisplay2();");

                    l.Text 
= MemoStringSlice(l.Text);
                }
            }
        }
原文地址:https://www.cnblogs.com/publicbill/p/414757.html