2012-05-07 16 views
1

私はC#WebアプリケーションにGridViewコントロールを持っています。私のグリッドビューでは、ButtonFieldのSelect、ID="btnSelect"があります。基本的に私のGridViewコントロールには、クライアントの名字、姓、住所、電話番号があり、対応する情報にはテキストボックスがあります。グリッドビューの選択ボタンを押したときに、クライアントの名前をテキストボックスに入れたいと思っていましたが、私はそれを成功させましたが、アプリケーションでは最大6つのクライアントを選択できます。私はこれをやっているよりも良い方法がありますか?以下のコードは次のとおりです。GridViewコントロールのRowコマンドで

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

これはページ上の任意のコントロールに対して機能します。 (別のコントロール内ではない)。 "txtAddr"、 "txtPhone"などの "txtName"を変更するだけです。私のコードでは、1行あたりのボタン数を想定しています。だから、クリックした行は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);オブジェクトの新しいインスタンスに設定されていないか、またはオブジェクトがnullかどうかを確認するためにチェックされません。私がしなければならないのは、適切なインデックスを見つけてそれをpopulateclientsに置くことだけです。 Btw、あなたはどのようにあなたのサブを働かせましたか? – Naina

+0

行インデックスとして渡すものは – Naina

+0

、コードの最初の行はgridviewrowです。 DisplayIndexをCommandArgumentとして渡してグリスを取得する必要はありません – naveen

関連する問題