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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
public class WebPartNews : System.Web.UI.WebControls.WebParts.WebPart
{
SPList liste ;
List<SPListItem> items ;
private Label index = new Label();
private Label titre = new Label();
private Label intro = new Label();
private Label contenu = new Label();
private Label urlImage = new Label();
private Label numerotation = new Label();
private Table table = new Table();
private UpdatePanel updatePanel;
private Button btnSuiv = new Button();
private Button btnPrec = new Button();
private LinkButton btnPlus = new LinkButton();
private string js;
public WebPartNews()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
HtmlGenericControl css = new HtmlGenericControl("link");
css.Attributes.Add("rel", "stylesheet");
css.Attributes.Add("href", SPContext.Current.Web.Url.ToString() + "/_layouts/WebPartNews/Style.css");
css.Attributes.Add("type", "text/css");
css.Attributes.Add("media", "screen");
css.Attributes.Add("charset", "utf-8");
this.Page.Header.Controls.Add(css);
}
protected override void OnLoad(EventArgs e)
{
this.EnsureChildControls();
}
public void refreshList()
{
liste = SPContext.Current.Web.Lists[Liste];
items = new List<SPListItem>();
foreach (SPListItem item in liste.Items)
{
items.Add(item);
}
index.Text = "0";
titre.Text = getFieldValue(index.Text, "Titre");
intro.Text = getFieldValue(index.Text, "Introduction");
contenu.Text = getFieldValue(index.Text, "Contenu");
urlImage.Text = "<img src='" + getUrlImgValue(index.Text, "Image") + "' alt='" + getUrlImgValue(index.Text, "Image") + "'/>";
numerotation.Text = int.Parse(index.Text) + 1 + " sur " + (items.Count);
}
string valeurChamp(SPListItem item, string colonne)
{
string res = null;
try
{
res = item[colonne].ToString();
}
catch
{
res = "Erreur avec : " + colonne;
}
return res;
}
string url(SPListItem item, string colonne)
{
SPFieldUrlValue value = new SPFieldUrlValue(item[colonne].ToString());
string url = value.Url;
return url;
}
protected override void CreateChildControls()
{
try
{
ScriptManager scriptHandler = ScriptManager.GetCurrent(Page);
if (scriptHandler == null)
{
scriptHandler = new ScriptManager();
}
refreshList();
btnPrec.ID = "refreshPrec";
btnPrec.Text = "Précédent";
btnPrec.Click += new EventHandler(btnPrec_Click);
btnPrec.Visible = false;
btnSuiv.Text = "Suivant";
btnSuiv.ID = "refreshSuiv";
btnSuiv.Click += new EventHandler(btnSuiv_Click);
table.ID = "idTable";
table.CssClass = "tableCss";
btnPlus.Text = "Lire la suite";
btnPlus.ID = this.ID + "voirPlus";
btnPlus.CssClass = "btnVoirPlus";
btnPlus.PostBackUrl = "#";
updatePanel = new UpdatePanel();
updatePanel.ID = "updatePanel";
updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
TableRow tr0 = new TableRow();
TableCell td0 = new TableCell();
TableCell td0a = new TableCell();
TableCell td0b = new TableCell();
TableRow tr1 = new TableRow();
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();
TableRow tr2 = new TableRow();
TableRow tr3 = new TableRow();
TableCell td3 = new TableCell();
TableCell td4 = new TableCell();
td0.Controls.Add(btnPrec);
td0.CssClass = "tdBtnPrec";
td0a.Controls.Add(numerotation);
td0a.CssClass = "tdNumerotation";
td0b.Controls.Add(btnSuiv);
td0b.CssClass = "tdBtnSuiv";
td1.Controls.Add(titre);
td1.ColumnSpan = 2;
td1.CssClass = "tdTitre";
td2.Controls.Add(urlImage);
td2.RowSpan = 3;
td3.Controls.Add(intro);
td3.Controls.Add(btnPlus);
td3.ColumnSpan = 2;
td3.CssClass = "tdIntroLien";
td4.Controls.Add(contenu);
td4.CssClass = "classContenu";
td4.ID = "Contenu";
td4.ColumnSpan = 2;
tr0.Cells.Add(td0);
tr0.Cells.Add(td0a);
tr0.Cells.Add(td0b);
tr1.Cells.Add(td1);
tr1.Cells.Add(td2);
tr2.Cells.Add(td3);
tr3.Cells.Add(td4);
table.Rows.Add(tr0);
table.Rows.Add(tr1);
table.Rows.Add(tr2);
table.Rows.Add(tr3);
updatePanel.ContentTemplateContainer.Controls.Add(table);
updatePanel.ContentTemplateContainer.Controls.Add(index);
//updatePanel.ContentTemplateContainer.Controls[1].Visible = false;
this.Controls.Add(scriptHandler);
this.Controls.Add(updatePanel);
js = string.Format(@"
<script type=""text/javascript"">
$(""#{0}"").hide();
$('#{1}').toggle(function(){{
$('#{0}').show('slow');
$('#{1}').text('{2}');
}},
function()
{{
$('#{0}').hide('slow');
$('#{1}').text('{3}');
}});
$('#{4}').click(function(){{
$('#{5}').fadeOut('slow');
}});
$('#{6}').click(function(){{
$('#{5}').hide('slow');
}});
</script>
",
td4.ClientID,
btnPlus.ClientID,
"Fermer",
"Lire la suite",
btnSuiv.ClientID,
table.ClientID,
btnPrec.ClientID
);
ScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), this.ID+"js", js, false);
}
catch (Exception e)
{
Label l = new Label();
l.Text = "La liste choisie ne correspond pas au type de WebPart";
Controls.Add(l);
}
}
void btnPrec_Click(object sender, EventArgs e)
{
int temp = int.Parse(index.Text);
temp--;
index.Text = temp.ToString();
miseAJour(index.Text);
if (temp < items.Count - 1)
{
btnSuiv.Visible = true;
}
else
{
btnSuiv.Visible = false;
}
if (temp > 0)
{
btnPrec.Visible = true;
}
else
{
btnPrec.Visible = false;
}
}
void btnSuiv_Click(object sender, EventArgs e)
{
int temp = int.Parse(index.Text);
temp++;
index.Text = temp.ToString();
miseAJour(index.Text);
if (temp < items.Count - 1)
{
btnSuiv.Visible = true;
}
else
{
btnSuiv.Visible = false;
}
if (temp > 0)
{
btnPrec.Visible = true;
}
else
{
btnPrec.Visible = false;
}
}
void miseAJour(string index)
{
titre.Text = getFieldValue(index, "Titre");
intro.Text = getFieldValue(index, "Introduction");
contenu.Text = getFieldValue(index, "Contenu");
urlImage.Text = "<img src='" + getUrlImgValue(index, "Image") + "' alt='" + getUrlImgValue(index, "Image") + "'/>";
numerotation.Text = int.Parse(index) + 1 + " sur " + (items.Count);
}
string getFieldValue(string index, string field)
{
string myValue = null;
try
{
myValue = valeurChamp(items[int.Parse(index)], field);
}
catch
{
myValue = null;
}
return myValue;
}
string getUrlImgValue(string index, string field)
{
string myValue = null;
try
{
myValue = url(items[int.Parse(index)], field);
}
catch
{
myValue = null;
}
return myValue;
} |