2012-05-07 4 views
1

C# 웹 응용 프로그램에 GridView 컨트롤이 있습니다. 내 gridview있는 ButtonField 선택, ID="btnSelect"라는 있습니다. 기본적으로 내 GridView 컨트롤에는 클라이언트 성, 성, 주소 및 전화 번호가 있으며 해당 정보에 대해서는 텍스트 상자가 있습니다. gridview에서 select 버튼을 누르거나 발동 할 때 클라이언트 이름을 텍스트 상자에 넣고 싶습니다. 성공적으로 완료했지만 응용 프로그램에서 최대 6 개의 클라이언트를 선택할 수 있습니다. 제가이 일을하는 것보다 더 좋은 방법이 있습니까? 코드는 다음과 같습니다 : 나는 행 명령에서 선택 버튼을 누르면 때마다, 나는 모든 사람이 만약 문이 복잡 넣어하지 않을 경우 곳으로이 코드에 더 좋은 방법은GridView 컨트롤의 행 명령

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 
    GridViewRow row = GridView1.Rows[index]; 


    if(string.IsNullOrEmpty(txtName1.Text) && string.IsNullOrEmpty(txtLName1.Text) && 
    string.IsNullOrEmpty(txtAddr1.Text) && string.IsNullOrEmpty(txtPhone1.Text)) 
    { 
     txtName1.Text=Server.HtmlDecode(row.Cells[1].Text); 
     txtLName1.Text=Server.HtmlDecode(row.Cells[2].Text); 
     txtAddr1.Text=Server.HtmlDecode(row.Cells[3].Text); 
     txtPhone1.Text=Server.HtmlDecode(row.Cells[4].Text); 

    } 
    //If I hit another select button then this will load the sencond set of txtboxes 
    if(string.IsNullOrEmpty(txtName2.Text) && string.IsNullOrEmpty(txtLName2.Text) && 
    string.IsNullOrEmpty(txtAddr2.Text) && string.IsNullOrEmpty(txtPhone2.Text)) 
    { 
     txtName2.Text=Server.HtmlDecode(row.Cells[1].Text); 
     txtLName2.Text=Server.HtmlDecode(row.Cells[2].Text); 
     txtAddr2.Text=Server.HtmlDecode(row.Cells[3].Text); 
     txtPhone2.Text=Server.HtmlDecode(row.Cells[4].Text); 

    } 
//The thrid time will load the third button and so on until I fill each txtbox if I choose. 
} 

있습니까? 이 핸들을 처리 할 수있는 foreach 루프가 있습니까? 모든 지침을 매우 높이 평가할 것입니다!

답변

0

FindControl 메서드를 살펴 보는 것이 좋습니다.

는 다음과 같은 사용할 수 :

TextBox txtName = FindControl(string.Format("txtName{0}", index) as TextBox; 
if(txtName != null) 
{ 
txtName.Text = row.Cells[1].Text; 
} 
+0

그것은'txtAddr2'와'txtPhone2'을 위해 일 것이다? – sarwar026

+0

페이지의 모든 컨트롤에서 작동합니다. (다른 컨트롤 안에 있지 않음). "txtName"을 "txtAddr", "txtPhone"등으로 변경해야합니다. 내 코드는 행당 버튼을 가정합니다. 그래서 클릭 한 행은 txt 상자의 행과 일치합니다 – mp3duck

+0

작동하도록했습니다! – Naina

0

최적화 된 버전은 여기에

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) { 
    GridViewRow row = ((Control) sender).NamingContainer as GridViewRow; 
    PopulateClients(txtName1, txtLName1, txtAddr1, txtPhone1, row); 

    //If I hit another select button then this will load the sencond set of txtboxes 
    PopulateClients(txtName2, txtLName2, txtAddr2, txtPhone2, row); 
    //The thrid time will load the third button and so on until I fill each txtbox if I choose. 
} 

private void PopulateClients(TextBox t1, TextBox t2, TextBox t3, TextBox t4, GridViewRow r) { 
    if (string.IsNullOrEmpty(t1.Text) && string.IsNullOrEmpty(t2.Text) && string.IsNullOrEmpty(t3.Text) && string.IsNullOrEmpty(t4.Text)) { 
     t1.Text = Server.HtmlDecode(r.Cells[1].Text); 
     t2.Text = Server.HtmlDecode(r.Cells[2].Text); 
     t3.Text = Server.HtmlDecode(r.Cells[3].Text); 
     t4.Text = Server.HtmlDecode(r.Cells[4].Text);  
    } 
}​ 
+0

이것은 대단합니다. 내 t1.Text = Server.HtmlDecode (r.Cells [1] .Text); object의 새로운 인스턴스로 설정되어 있지 않은지, object가 null인지 어떤지를 조사하고 있습니다. 올바른 색인을 찾아서 그것을 populateclients에 두는 것만 큼 나는 것처럼 보입니다. Btw, 어떻게 아빠를 일하게 만들었 니? – Naina

+0

행 색인으로 전달하는 대상은 어디입니까? – Naina

+0

코드의 첫 번째 행은 gridviewrow를 제공합니다. DisplayIndex를 CommandArgument로 전달하고 그놈을 가져올 필요가 없습니다. – naveen