2011-11-18 49 views

回答

1

一旦你赶上正确的项目,您可以使用该项目的FindControl方法,像这样:

var myLabel = item.FindControl("telephoneidLabel") as Label; 

if(myLabel != null) 
{ 
    var myText = myLabel.Text; 
} 

的,你得到的item对象取决于你的代码是如何,如果你在整个formview控制上做一个循环,或者你得到editItemselectedItem等......但是FindControl,它的用法总是一样的。

+0

感谢名单达维德VAR telLabel = form1.FindControl(“telephoneidL阿贝尔“)作为标签; string strtel = telLabel.Text;有效! – debutante

0

这很容易做到 - 现在双向绑定应该会自动为您执行此操作,假设您使用GridView作为数据源的SqlDataSource。下面是一些代码,我对SqlDataSource的:现在

UpdateCommand="UPDATE [prov] SET [email protected], [email protected], [email protected] WHERE [email protected] " >  
    <UpdateParameters> 
     <asp:ControlParameter ControlID="dgProviders" Name="provID" PropertyName="SelectedValue" /> 
     <asp:Parameter Name="login" /> 
     <asp:Parameter Name="password" /> 
     <asp:Parameter Name="username" /> 
     <asp:Parameter Name="contact_email" /> 
     <asp:Parameter Name="bar_number" /> 
    </UpdateParameters> 

,如果你想获得的标签对象,然后在approproate事件处理程序,您可以使用下面的代码为例:

Protected Sub dgProviders_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgProviders.RowCommand 
    Select e.CommandName 
     Case "Search" 
      Dim strProvName As String = CType(Me.dgProviders.HeaderRow.FindControl("txtSearchName"), TextBox).Text 
      If strProvName = String.Empty Then 
       Me.lblResults.Text = "<span style=""color:maroon"">You have to enter a search term: part of the name to do a search.</span><br />" 
      Else 
       Me.sqlProvList.SelectParameters.Clear() 
       If strProvName <> String.Empty Then 
        Me.sqlProvList.SelectCommand = "SELECT provID, provname, addr, tele FROM prov WHERE [provname] LIKE '%' + @username + '%' ORDER BY [provname]" 
        Me.sqlProvList.SelectParameters.Add("username", DbType.String, strProvName) 
       End If 
       Me.dgProviders.PageIndex = 0 
       Session("Select") = Me.sqlProvList.SelectCommand 
      End If 
    End Select 
End Sub 

这是从我有一个“搜索”按钮,从数据网格的标题行中的文本框中获取值。

+0

你在底部有一个巨大的,无格式的代码块... –

0

假设该行是在编辑模式下,你应该能够从这样的控制获得的价值:我想知道,如果它可能是最好使用datakey这个

//the row that's being edited 
GridViewRow row = GridView1.Rows[0]; 

if (row.RowState == DataControlRowState.Edit) 
{ 
    Label lblCtrl = row.FindControl("Label1") as Label; 
    if (lblCtrl != null) 
    { 
     string text = lblCtrl.Text; 
    } 
} 

,虽然:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeColumn" ...> 

代码隐藏:

string someValue = (string)GridView1.DataKeys[0]["SomeColumn"]; 
相关问题