Ajouter dynamiquement un bouton radio à un gridview
Bonjour, j'ai un grand problème, ça fait une journée que cherche sur le net mais malheureusement sans résultat. Je souhaite ajouter à mon gridview un bouton radio à chaque ligne.
- Mon grid est contenu dans une webpart dans le code utilisé est uniquement c#.
- Je dois gérer le post back quand je sélectionne un radio.
- Je dois sélectionner une seule ligne comme en HTML en spécifiant le nom, mais avec asp.net c'est pas faisable.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace ProjetSPGridView
{
class RadioButtonTemplateField : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private SPGridView grid;
public RadioButtonTemplateField(DataControlRowType type, string colname,SPGridView grid)
{
templateType = type;
columnName = colname;
this.grid = grid;
}
void ITemplate.InstantiateIn(Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
RadioButton rb = new RadioButton();
rb.AutoPostBack = true;
rb.ID = "rb1";
rb.EnableViewState = true;
//DataTable dt = (DataTable)grid.DataSource;
//rb.Text = DataBinder.Eval(container.i;
rb.DataBinding += delegate(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)rb.NamingContainer;
int ID = (int)DataBinder.Eval(row.DataItem, "ID");
((RadioButton)sender).Text = ID.ToString();
};
rb.CheckedChanged += new EventHandler(rb_CheckedChanged);
//LiteralControl c = new LiteralControl("radio");
//c.ID = "radio";
//c.Controls.Add(rb);
//rb.Text = "radio";
//rb.Items.Add("aa");
//rb.SelectedIndex = 0;
//rb.DataValueField=DataBinder.Eval(container.data
//container.Controls.Add(rb);
container.Controls.Add(rb);
break;
default:
break;
}
}
void rb_CheckedChanged(object sender, EventArgs e)
{
((Page)sender).Response.Write("<script>bonjour</script>");
RadioButton rb = new RadioButton();
rb = (RadioButton)sender;
string str = rb.Text;
SPGridView grid = (SPGridView)sender;
RadioButton rbgrid;
for (int i = 0; i < grid.Rows.Count; i++)
{
rbgrid = (RadioButton)grid.Rows[i].FindControl("rb1");
rbgrid.Checked = false;
if (str == rbgrid.Text)
{
rbgrid.Checked = true;
((Page)sender).Session["nomCheck"] = rbgrid.Text;
}
}
}
}
} |
Le code de la webpart :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
protected override void CreateChildControls()
{
base.CreateChildControls();
GetData();
oView.Table = datatable;
oGrid.ID = "UserGrid";
oGrid.DataSource = oView;
oGrid.AutoGenerateColumns = false;
oGrid.RowDataBound +=new GridViewRowEventHandler(oGrid_RowDataBound);
//oGrid.RowDataBound += new EventHandler(oGrid_DataBound);
oGrid.AllowSorting = true;
oGrid.RowCreated += new GridViewRowEventHandler(oGrid_RowCreated);
oGrid.SelectedIndexChanged += new EventHandler(oGrid_SelectedIndexChanged);
oGrid.SelectedIndexChanging += new GridViewSelectEventHandler(oGrid_SelectedIndexChanging);
oGrid.CssClass = "ms-listviewtable";
oGrid.AlternatingRowStyle.CssClass = "ms-alternating";
oGrid.Width = new Unit(50, UnitType.Percentage);
oGrid.GridLines = GridLines.None;
oGrid.HeaderStyle.Font.Bold = true;
oGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
BoundField colTitle;
for (int i = 0; i < datatable.Columns.Count; i++)
{
colTitle = new BoundField();
colTitle.DataField = datatable.Columns[i].ColumnName;
colTitle.HeaderText = datatable.Columns[i].ColumnName;
colTitle.SortExpression = datatable.Columns[i].ColumnName;
this.oGrid.Columns.Add(colTitle);
}
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
customField.HeaderTemplate = new RadioButtonTemplateField(DataControlRowType.Header, "", oGrid);
customField.ItemTemplate = new RadioButtonTemplateField(DataControlRowType.DataRow, " ", oGrid);
oGrid.Columns.Add(customField);
this.Controls.Add(oGrid);
}
void oGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
//SPGridViewRow row = (SPGridViewRow)sender;
//if(oGrid.Rows[0]==row)
//{
// RadioButton rbtnl = (RadioButton)e.Row.FindControl("rb1");
// rbtnl.Checked = true;
// }
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rbtnl = (RadioButton)e.Row.FindControl("rb1");
if (rbtnl.Text.Equals(Page.Session["nomCheck"]))
rbtnl.Checked = true;
}
}
void oGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Page.Response.Write("<script>e.NewSelectedIndex</script>") ;
}
void oGrid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void oGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rbtnl = (RadioButton)e.Row.FindControl("rb1");
if (rbtnl.Text.Equals(Page.Session["nomCheck"]))
rbtnl.Checked = true;
}
} |