如何点击链接按钮的事件并在单击GridView的linkBut​​ton时显示模态弹出窗口 [英] How to fire Click Event of link button and showing Modal Popup when click on linkButton of GridView

查看:75
本文介绍了如何点击链接按钮的事件并在单击GridView的linkBut​​ton时显示模态弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们尝试使用asp.net中的模板字段通过代码构建基于数据源的动态列构建gridview,因为no.of要显示的列可能每月都有所不同。

为此,为了实现,我们开发了一个实现ITemplate接口的类DynamicTemplate。在该模板字段中,我已在每个单元格中插入LinkBut​​ton,当我单击该单元格链接按钮时,我需要显示具有所选单元格值的一个弹出窗口。


为此,我创建了一个Default.asxp页面并编写了以下内容。



详细样本请从此链接下载 [ ^ ]



  public  部分 默认值:System.Web.UI.Page 
{
DataTable dt;

受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
GenateGridView();

}

private void GenateGridView()
{
TemplateField tempField;
DynamicTemplate dynTempItem;
LinkBut​​ton lnkBut​​ton;
标签标签;

GridView gvDynamicArticle = new GridView();

gvDynamicArticle.Width = Unit.Pixel( 500 );
gvDynamicArticle.BorderWidth = Unit.Pixel( 0 );
gvDynamicArticle.Caption = < div>默认网格< / div>;
gvDynamicArticle.AutoGenerateColumns = false ;

DataTable data = getBindingData();

for int i = 0 ; i < data.Columns.Count; i ++)
{
tempField = new TemplateField();
dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);

lnkBut​​ton = new LinkBut​​ton();

lnkBut​​ton.ID = string .Format( lnkBut​​ton {0},i);
lnkBut​​ton.Visible = true ;

string ColumnValue = data.Columns [i] .ColumnName;
tempField.HeaderText = ColumnValue;

if (ColumnValue == EmpName
{
label = new Label();

label.ID = string .Format( Label {0},i);
dynTempItem.AddControl(label, Text,ColumnValue);
label.Width = 100 ;
}
else
{
dynTempItem.AddControl(lnkBut​​ton, Text,ColumnValue);
lnkBut​​ton.Click + = lnkBut​​ton_Click;
}
tempField.ItemTemplate = dynTempItem;
gvDynamicArticle.Columns.Add(tempField);
/// /// grdUserPivotDateTwo.Columns.Add(tempField);
}

gvDynamicArticle.DataSource = data;
gvDynamicArticle.DataBind();

divContainer.Controls.Add(gvDynamicArticle);

}

void lnkBut​​ton_Click( object sender ,EventArgs e)
{
// 在popUp中显示单元格值..
ClientScript.RegisterStartupScript( this .GetType(), alert alert('cell clicked'));
}


private DataTable getBindingData()
{
dt = new DataTable();
dt.Columns.Add( new DataColumn( EmpName));
dt.Columns.Add( new DataColumn( 星期一));
dt.Columns.Add( new DataColumn( 星期二));
dt.Columns.Add( new DataColumn( 星期三));
dt.Columns.Add( new DataColumn( 星期四));

dt.Rows.Add( EmpOne p p p a);
dt.Rows.Add( EmpTwo p a p p);
dt.Rows.Add( EmpThree p p p a);
dt.Rows.Add( EmpFour p a p p);
dt.Rows.Add( EmpFive p p p a);
dt.Rows.Add( EmpSix a p p p);

return dt;

}



}





和相应的DynamicTemplate类是



  public   class  DynamicTemplate:System.Web.UI.ITemplate 
{
System.Web.UI.WebControls.ListItemType templateType;
System.Collections.Hashtable htControls = new System.Collections.Hashtable();
System.Collections.Hashtable htBindPropertiesNames = new System.Collections.Hashtable();
System.Collections.Hashtable htBindExpression = new System.Collections.Hashtable();

public DynamicTemplate(System.Web.UI.WebControls.ListItemType type)
{
templateType = type;
}

public void AddControl(WebControl wbControl, String BindPropertyName, String BindExpression)
{
htControls.Add(htControls.Count,wbControl) );
htBindPropertiesNames.Add(htBindPropertiesNames.Count,BindPropertyName);
htBindExpression.Add(htBindExpression.Count,BindExpression);

}

public void InstantiateIn(系统.Web.UI.Control容器)
{
PlaceHolder ph = new PlaceHolder();

for int i = 0 ; i < htControls.Count; i ++)
{

// 克隆控制
控制cntrl = CloneControl((控制)htControls [i]);

switch (templateType)
{
case ListItemType.Header:
break ;
case ListItemType.Item:
ph.Controls.Add(cntrl);
break ;
case ListItemType.AlternatingItem:
ph.Controls.Add(cntrl);
ph.DataBinding + = new EventHandler(Item_DataBinding);
break ;
case ListItemType.Footer:
break ;
}
}
ph.DataBinding + = new EventHandler(Item_DataBinding);

container.Controls.Add(ph);

}
public void Item_DataBinding( object sender,System.EventArgs e)
{
PlaceHolder ph =(PlaceHolder)sender;
GridViewRow ri =(GridViewRow)ph.NamingContainer;
for int i = 0 ; i < htControls.Count; i ++)
{
if (htBindPropertiesNames [i] .ToString()。Length > 0
{
控制tmpCtrl =(控制)htControls [i];
String item1Value =( String )DataBinder.Eval(ri.DataItem,htBindExpression [i]的ToString());
控制ctrl = ph.FindControl(tmpCtrl.ID);

类型t = ctrl.GetType();
System.Reflection.PropertyInfo pi = t.GetProperty(htBindPropertiesNames [i] .ToString());

pi.SetValue(ctrl,item1Value.ToString(), null );
}

}



}

私人 Control CloneControl(System.Web.UI.Control src_ctl)
{
Type t = src_ctl.GetType();
Object obj = Activator.CreateInstance(t);
控制dst_ctl =(控制)obj;
PropertyDescriptorCollection src_pdc = TypeDescriptor.GetProperties(src_ctl);
PropertyDescriptorCollection dst_pdc = TypeDescriptor.GetProperties(dst_ctl);

for int i = 0 ; i < src_pdc.Count; i ++)
{

if (src_pdc [i] .Attributes.Contains(DesignerSerializationVisibilityAttribute.Content))
{

object collection_val = src_pdc [i] .GetValue(src_ctl);
if ((collection_val IList)== true
{
foreach object child (IList)collection_val中的class =code-keyword>
{
控制new_child = CloneControl(child as Control) ;
object dst_collection_val = dst_pdc [i] .GetValue(dst_ctl);
((IList)dst_collection_val).Add(new_child);
}
}
}
else
{
dst_pdc [src_pdc [i]。名称] .SetValue(dst_ctl,src_pdc [i] .GetValue(src_ctl));
}
}

return dst_ctl;

}
}







这里在gridview中显示的数据很好

这里 问题是
当我点击linkBut​​ton页面重新加载并且在回发后没有显示网格。

LinkBut​​ton点击事件未触发。

请提供帮助全部信息/示例以显示模态窗口时我们点击gridview的linkBut​​ton。

解决方案

点击网格视图中的编辑按钮后面的代码打开模态弹出窗口

  protected   void  lnkBut​​ton_Click(( object  sender,EventArgs e)
{
hfUser.Value = 0;
GridViewRow gvRow =(GridViewRow)((LinkBut​​ton)sender).NamingContainer;
int userId = Convert.ToInt32(gvUser.DataKeys [gvRow.RowIndex] .Value);
// 从数据库获取代码
// 填写所有控件并按
// 在popUp中显示单元格值..
// ClientScript.RegisterStartupScript(this.GetType(),alert,alert('cell clicked'));
mpeUserDetail.Show() ;
}





获取更多信息

服务器端代码中的ModealPopupExtender [ ^ ]

Ajax的modalpopupextender出现隐藏从 - 代码隐藏和 - 通过JavaScript的 [<一个href =http://www.advancesharp.com/blog/1059/ajax-modalpopupextender-show-hide-from-code-behind-and-by-javascripttarget =_ blanktitle =New Window> ^ ]


 Hi,

 we have tried to build gridview with dynamic columns based the data source using the template fields in asp.net through code behind because the no.of columns to display may vary each month.

For that, to implement we have developed one class "DynamicTemplate" which implements the "ITemplate" interface. In that template fields i have inserted the "LinkButton" in each cell and when i click that cell link button i need to show the one Popup with selected cell value.


For that i have created one Default.asxp page and wrote the following.


For Detailed Sample Please download from this link [^]

public partial class Default : System.Web.UI.Page
   {
       DataTable dt;

       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
               GenateGridView();

       }

       private void GenateGridView()
       {
           TemplateField tempField;
           DynamicTemplate dynTempItem;
           LinkButton lnkButton;
           Label label;

           GridView gvDynamicArticle = new GridView();

           gvDynamicArticle.Width = Unit.Pixel(500);
           gvDynamicArticle.BorderWidth = Unit.Pixel(0);
           gvDynamicArticle.Caption = "<div>Default Grid</div>";
           gvDynamicArticle.AutoGenerateColumns = false;

           DataTable data = getBindingData();

           for (int i = 0; i < data.Columns.Count; i++)
           {
               tempField = new TemplateField();
               dynTempItem = new DynamicTemplate(ListItemType.AlternatingItem);

               lnkButton = new LinkButton();

               lnkButton.ID = string.Format("lnkButton{0}", i);
               lnkButton.Visible = true;

               string ColumnValue = data.Columns[i].ColumnName;
               tempField.HeaderText = ColumnValue;

               if (ColumnValue == "EmpName")
               {
                   label = new Label();

                   label.ID = string.Format("Label{0}", i);
                   dynTempItem.AddControl(label, "Text", ColumnValue);
                   label.Width = 100;
               }
               else
               {
                   dynTempItem.AddControl(lnkButton, "Text", ColumnValue);
                   lnkButton.Click += lnkButton_Click;
               }
               tempField.ItemTemplate = dynTempItem;
               gvDynamicArticle.Columns.Add(tempField);
               //////grdUserPivotDateTwo.Columns.Add(tempField);
           }

           gvDynamicArticle.DataSource = data;
           gvDynamicArticle.DataBind();

           divContainer.Controls.Add(gvDynamicArticle);

       }

       void lnkButton_Click(object sender, EventArgs e)
       {
           // showing cell values in popUp here..
           ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked')");
       }


       private DataTable getBindingData()
       {
           dt = new DataTable();
           dt.Columns.Add(new DataColumn("EmpName"));
           dt.Columns.Add(new DataColumn("Monday"));
           dt.Columns.Add(new DataColumn("TuesDay"));
           dt.Columns.Add(new DataColumn("WednesDay"));
           dt.Columns.Add(new DataColumn("ThursDay"));

           dt.Rows.Add("EmpOne", "p", "p", "p", "a");
           dt.Rows.Add("EmpTwo", "p", "a", "p", "p");
           dt.Rows.Add("EmpThree", "p", "p", "p", "a");
           dt.Rows.Add("EmpFour", "p", "a", "p", "p");
           dt.Rows.Add("EmpFive", "p", "p", "p", "a");
           dt.Rows.Add("EmpSix", "a", "p", "p", "p");

           return dt;

       }



   }



and corresponding DynamicTemplate class is


public class DynamicTemplate : System.Web.UI.ITemplate
{
    System.Web.UI.WebControls.ListItemType templateType;
    System.Collections.Hashtable htControls = new System.Collections.Hashtable();
    System.Collections.Hashtable htBindPropertiesNames = new System.Collections.Hashtable();
    System.Collections.Hashtable htBindExpression = new System.Collections.Hashtable();

    public DynamicTemplate(System.Web.UI.WebControls.ListItemType type)
    {
        templateType = type;
    }

    public void AddControl(WebControl wbControl, String BindPropertyName, String BindExpression)
    {
        htControls.Add(htControls.Count, wbControl);
        htBindPropertiesNames.Add(htBindPropertiesNames.Count, BindPropertyName);
        htBindExpression.Add(htBindExpression.Count, BindExpression);

    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        PlaceHolder ph = new PlaceHolder();

        for (int i = 0; i < htControls.Count; i++)
        {

            //clone control 
            Control cntrl = CloneControl((Control)htControls[i]);

            switch (templateType)
            {
                case ListItemType.Header:
                    break;
                case ListItemType.Item:
                    ph.Controls.Add(cntrl);
                    break;
                case ListItemType.AlternatingItem:
                    ph.Controls.Add(cntrl);
                    ph.DataBinding += new EventHandler(Item_DataBinding);
                    break;
                case ListItemType.Footer:
                    break;
            }
        }
        ph.DataBinding += new EventHandler(Item_DataBinding);

        container.Controls.Add(ph);

    }
    public void Item_DataBinding(object sender, System.EventArgs e)
    {
        PlaceHolder ph = (PlaceHolder)sender;
        GridViewRow ri = (GridViewRow)ph.NamingContainer;
        for (int i = 0; i < htControls.Count; i++)
        {
            if (htBindPropertiesNames[i].ToString().Length > 0)
            {
                Control tmpCtrl = (Control)htControls[i];
                String item1Value = (String)DataBinder.Eval(ri.DataItem, htBindExpression[i].ToString());
                Control ctrl = ph.FindControl(tmpCtrl.ID);

                Type t = ctrl.GetType();
                System.Reflection.PropertyInfo pi = t.GetProperty(htBindPropertiesNames[i].ToString());

                pi.SetValue(ctrl, item1Value.ToString(), null);
            }

        }



    }

    private Control CloneControl(System.Web.UI.Control src_ctl)
    {
        Type t = src_ctl.GetType();
        Object obj = Activator.CreateInstance(t);
        Control dst_ctl = (Control)obj;
        PropertyDescriptorCollection src_pdc = TypeDescriptor.GetProperties(src_ctl);
        PropertyDescriptorCollection dst_pdc = TypeDescriptor.GetProperties(dst_ctl);

        for (int i = 0; i < src_pdc.Count; i++)
        {

            if (src_pdc[i].Attributes.Contains(DesignerSerializationVisibilityAttribute.Content))
            {

                object collection_val = src_pdc[i].GetValue(src_ctl);
                if ((collection_val is IList) == true)
                {
                    foreach (object child in (IList)collection_val)
                    {
                        Control new_child = CloneControl(child as Control);
                        object dst_collection_val = dst_pdc[i].GetValue(dst_ctl);
                        ((IList)dst_collection_val).Add(new_child);
                    }
                }
            }
            else
            {
                dst_pdc[src_pdc[i].Name].SetValue(dst_ctl, src_pdc[i].GetValue(src_ctl));
            }
        }

        return dst_ctl;

    }
}




 Here the Data showing in gridview is fine

 Here the Issues are 
when i click on the linkButton the page reloads and no grid is displaying after the postback.

and for LinkButton the "Click Event" is not firing.

Please provide me the help full information/Sample to show the modal window when we click on the linkButton of the gridview. 

解决方案

open the modal popup on clicking the edit button in grid view from code behind

protected void lnkButton_Click((object sender, EventArgs e)
{
    hfUser.Value = "0";
    GridViewRow gvRow = (GridViewRow)((LinkButton)sender).NamingContainer;
    int userId = Convert.ToInt32(gvUser.DataKeys[gvRow.RowIndex].Value);
     //get code from database
     // fill all the controls and open modal popup by
// showing cell values in popUp here.. 
//ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('cell clicked')");
    mpeUserDetail.Show();
}



Get more info
ModalPopupExtender from Server Side Code[^]
ajax-modalpopupextender-show-hide-from-code-behind-and-by-javascript[^]


这篇关于如何点击链接按钮的事件并在单击GridView的linkBut​​ton时显示模态弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆